Use function in app.php to build url for image html - understanding scope/templates

Hello, feeling my way around sage / blade etc for the first time and running into an issue, just not sure where my logic is flawed, perhaps in a few places :upside_down_face:

I have groups of child pages that need to display the same image as the parent. In the app/Controllers/App.php file I have made a function that returns the correct section name slug which I then hope to use to write out the HTML for the image. The image name contains the slug of the parent.

public static function sectionslug()
{
    if (is_page() ){
        if (is_front_page()) {
            $sectionslug = "home";
        }else if (get_post_parent()){
            $sectionslug = basename(get_permalink(get_post_parent()));
        }else{
            $sectionslug = basename(get_permalink());
        }
        
    }
   return $sectionslug;
}

In header.blade.php file when I call this it correctly writes out the section name which I want to use on the front end.

 {!! App::sectionslug() !!} 

BUT, when I try to concatenate this into a string to make the filename I’m not getting anywhere. Not sure what the syntax should be, or If I’m missing a step somewhere, or if I’m off on a tangent. Here’s an example of just one of the many ways that I’ve tried so far that doesn’t work.

<img src=@asset("images/banners/banner-{!! App::sectionslug() !!}.svg")> 
<img src=@asset({{"images/banners/banner-".sectionslug().".svg"}})> 

Help really appreciated! Also, if this is a daft way to go about it and you can suggest an alternative method I’m all ears!

Things that start with @ like @asset are directives which are a feature of Blade. The important thing to remember about directives is that they are not PHP–they are instructions for how the Blade engine can generate some PHP when it renders the template. Blade is a sort of markup that is processed by the Blade engine to generate PHP–the Blade templates you write are never actually executed to serve pages. They are processed by the Blade engine to generate PHP files, and it’s those files that are actually used by WordPress, etc, to build and display your pages.

The other important thing to remember about directives is that, whatever it may look like, they take only one “argument” and it’s a string. It can (and often will) output that string as PHP, but that means that what you pass to it must be valid PHP. {!! something() !!} is not valid PHP–that’s Blade syntax, which PHP will choke on.

In your case, you need to pass something that can be interpreted as PHP when it, so something like this:

@asset('"images/banners/banner-" . App::sectionslug() . ".svg"')

(I don’t know if that will work; I wrote it from memory and I have messed with directives in this way in a while.)

I’d recommend looking at the templates that the Blade engine generates to get an idea of how its internals actually work–that’s what helped me figure it out. I forget where exactly Sage 9 puts them, but I think it’s in wp-content somewhere. Look for a directory with a bunch of alphabet-soup-named PHP files.

1 Like

Thank you very much! Your memory was almost perfect, the correct syntax in the end was.

 <img src= @asset("images/banners/banner-" . App::sectionslug() . ".svg") >

Happy days! I’ll check out that alphabet soup too.

This topic was automatically closed after 42 days. New replies are no longer allowed.