Using @asset with php variables?


#1

I’m trying to use the shorthand @asset in an acf repeater to dynamically load assets. Is this possible?


#2

@asset is used to load assets that are part of your theme, not images that are stored in WordPress’s media library. Unless your ACF repeater is passing strings that point to images in your theme, no, it will not work.

Most of the functionality in Sage is pretty straightforward, and with a little elbow grease you can figure out what it’s doing and exactly how it works. For instance, I worked back through the files to determine exactly how @asset worked to answer this question:

It’s defined in theme/app/setup.php:

/**
 * Create @asset() Blade directive
 */
sage('blade')->compiler()->directive('asset', function ($asset) {
    return "<?= " . __NAMESPACE__ . "\\asset_path({$asset}); ?>";
});

Essentially, it prints out a PHP call to a function in the App namespace called asset_path(). You can find that function in theme/app/helpers.php:

/**
 * @param $asset
 * @return string
 */
function asset_path($asset)
{
    return sage('assets')->getUri($asset);
}

asset_path() is using an object returned by sage() to call the getUri() method. sage() is also defined in helpers.php:

use Roots\Sage\Container;
/**
 * Get the sage container.
 *
 * @param string $abstract
 * @param array  $parameters
 * @param Container $container
 * @return Container|mixed
 */
function sage($abstract = null, $parameters = [], Container $container = null)
{
    $container = $container ?: Container::getInstance();
    if (!$abstract) {
        return $container;
    }
    return $container->bound($abstract)
        ? $container->makeWith($abstract, $parameters)
        : $container->makeWith("sage.{$abstract}", $parameters);
}

sage() is returning a Container object, and by looking at the top of the file, we can see that container is coming from use Roots\Sage\Container. A little bit of reasoning (or help from your IDE) will tell you that class is defined in the sage-lib composer package, which means you can find it in theme/vendor/roots/sage-lib/Assets/JsonManifest.php. And sure enough, there’s the getUri() method:

/** @inheritdoc */
public function getUri($asset)
{
    return "{$this->dist}/{$this->get($asset)}";
}

We can look at this method and see that it is concatenating the string for the dist folder with the string that we passed to it as our asset. This means that, while we could theoretically pass a string with lots of ../s in it and somehow navigate to the WordPress media gallery, that’s not really the intent of the method.

If you want to use directives for your loop, you could pretty easily create your own directive to wrap wp_get_attachment_image() or a similar function.


#3

Thanks for the in-depth explanation. So, yes I’m passing strings with ACF that point to icons in my theme. I don’t want to put icons in the media library so maybe I should look for another method to do this properly.


#4

If you’re using icons that are in your theme files (i.e. theme/resources/assets/), then @asset would work. It won’t work for items that are stored in WP’s media library (i.e. /wp-content/uploads/). Sorry if that wasn’t clear.


#5

It’s very clear. I think it’s me who haven’t been clear. My bad.

I have some icons in the images folder of my theme. Let’s say cog.svg and key.svg.

I’m using ACF text fields inside a repeater where I type cog or key so my theme know which one to load and where.

I was wondering if there is a possibility to use the @asset shorthand with a php variable that gets the ACF field inside a repeater. Example (which doesn’t work):

@php $var = get_sub_field('icon') @endphp
<img src="@asset('images/icons/{{ $var }}.svg')">

#6

I would process your data at the controller level to create the entire path, then pass a single variable to @asset:

// your controller puts this into $processed_icons
return array_map(function($item) {
   return "images/icons/$item.svg";
}, $icons);

// your partial
@foreach($processed_icons as $icon)
   <img src="@asset($icon)">
@endforeach

@asset will print whatever you give it into the PHP call it generates: The code you posted would (I believe) result in:

<img src="dist/images/icons{{$var}}.svg">

…Which PHP cannot process (it doesn’t understand Blade echo tags).

One of the most valuable tools I’ve found for debugging/figuring out Blade is to look at the PHP files Blade generates from your templates. Seeing what PHP they result it often helps to make it clear how various Blade things work. Unless you’ve changed the configuration, you should be able to find those files in /wp-content/uploads/cache.


#7

You’re awesome. Thanks!


#8

Glad I could help! If you can, please mark the post that solved it as the answer to help other folks.