Blade Directives for Sage

I’m using Sage 10 and installed the sage directives composer dependency. When I try to use @guest I get this error:

ErrorException: Call to undefined function auth() (View: /srv/www/biolayne.com/current/web/app/themes/biolayne_3.0.0/resources/views/partials/home/hero.blade.php) (View: /srv/www/biolayne.com/current/web/app/themes/biolayne_3.0.0/resources/views/partials/home/hero.blade.php) in /srv/www/biolayne.com/current/web/app/uploads/cache/6073fe26b0e42fc88197e41abb3990f11754f7f4.php on line 39

Is there another step I’m missing to get this package hooked up to Sage10? Thanks!

Did you install it in your theme folder or in bedrock?

Doh, wrong folder :man_facepalming:

1 Like

Hi! Thanks so much for this, great stuff.

Is there a way to set the image size when doing something like:

@field('bg_image', 'url')

Would like to grab the ‘large’ size instead of ‘full’.

You might be looking for @image

1 Like

Seems you can’t just return the url of an image size other than raw - all other sizes output an img tag. I’m using the image as inline css background-image so just want the url.

Encountered a caching issue when using @image to grab an acf image field on CPT posts – just loads the first post you visit’s field value until you delete the uploads/cache directory. Then it will load only the current post’s field value until you clear the cache again. Using wp_get_attachment_image doesn’t have the issue.

Am using stoutlogic’s acf-builder, trellis and bedrock.

Here’s my field:

namespace App;

use StoutLogic\AcfBuilder\FieldsBuilder;

$project = new FieldsBuilder('project');

$project
    ->setLocation('post_type', '==', 'project');

$project
    ->addTab('section_2', ['placement' => 'left'])
        ->addImage('section_2_image_1', ['label' => 'Image 1', 'return_format' => 'id'])
        ->addImage('section_2_image_2', ['label' => 'Image 2', 'return_format' => 'id'])
        ->addImage('section_2_image_3', ['label' => 'Image 3', 'return_format' => 'id'])
return $project;

In my single-project.blade.php:

@image('section_2_image_1', 'large')

Edit added my code

Probably a stupid question, can you use @hasfield to check for multiple fields?

Hi.

I’m wondering what’s the easiest way to use @thumbnail and to add a custom class to it?

I need to keep the srcset markup.

Would someone have an idea, please?

I too am getting cache issues when using @image

I am using ACF pro and have image based fields using ID as the data value. When saving a value (image file) to the field and viewing the page, I see the correct image. When editing the same page and deleting/adding a new value/image file to that same field, it updates and shows correctly in the back end, however the front end displays the previous image. Deleting the /uploads/cache folder makes the page display the latest and correct value.

Had to resort to using wp_get_attachment_image

Directives are not executed as PHP; they’re essentially instructions that tell the Blade engine to generate particular PHP when it renders the cached blades. That means you need to be careful with what/how data is passed to them. It sounds like either the directive you’re using or the way you’re passing data to it is causing the ID value present when the cached blade is generated to be “baked in” as a scalar value, not a variable to be evaluated at runtime.

You can see how @image works here: https://github.com/Log1x/sage-directives/blob/145ef53d730c37d81e37f1b9ed9ec494be3fd922/src/Directives/WordPress.php#L357-L402

I’d also recommend taking a look at the generated PHP that actually appears in your cached blades to see what it’s doing.

Any tips on how to retrieve Image IDs from a gallery field using only @fields(‘gallery’)?

I’ve been storing the gallery field in a variable, then looping through it — but I’m wondering if there’s a cleaner way to go.

I’ve installed this in Sage 10 as per the instructions but when I use for example @field, it results in a blank page. I’d appreciate any pointers in the right direction. :slight_smile:

EDIT:
Nevermind, I’m an idiot. I actually wrote {{ @field('test) }} rather than @field(‘test’). :man_facepalming::smile:

If I need to add a default image, is this correct?

@if ( has_post_thumbnail() ) 
    <img src="@thumbnail('full', false)" alt="Full Image" />
 @else 
    <img src="images/thumbnail-default.jpg" />
@endif

Because I used @if (thumbnail() ) is wrong.

@Log1x, thanks so much for this project! It’s been fun to implement across all my recent projects.

I’ve got a question that might seem silly… With ACF fields, to echo a field from a specific post = @field('text', 1). Is that for ACF Pro only? I’m assuming this uses the Global Fields feature that’s on the pro license?

Tried to do it on the free version, and I get
Notice: Trying to access array offset on value of type null in /srv/www/domain.com/current/web/app/uploads/cache/e9dd69eb86fcd8e1a7fd5f631109a6b2ebcfa139.php on line 34

I have no real experience in using Log1x/sage.directives but it seems to me that @field is using ACFs get_field() which is not a pro feature. See sage-directives/ACF.php at master · Log1x/sage-directives · GitHub .

What do you get if you @dump / var_dump() the value of get_field('text', 1) in your template?

If you have updated ACF to 5.11, maybe you are running in to the same issue as raised at get_field returns null after upgrading to v5.11 · Issue #570 · AdvancedCustomFields/acf · GitHub?

2 Likes

What was the blade directive parsed into on line 34 of that view cache file?

I believe this may be related to the issue I found and similar to #44
and #63

The check if the second parameter to the directive is an ID is using !is_string however blade is always parsing strings. … I have a PR out there to fix it. Fix issues with field, hasfield, isfield post ID parsing by EHLOVader · Pull Request #68 · Log1x/sage-directives · GitHub

If you wanted to try it out, you can use composer-patches to apply it before it is merged.

2 Likes

Is there a way I can include an svg using the returned value via @sub as shown below? I couldn’t set it to a variable successfully

@options('socials')
	<a href="@sub('social_link')" class="banner__socials-link">
		@include("SVG::@sub('social_icon')-icon")
	</a>
@endoptions

Hi @Log1x ,

Thanks so much for your work on this, I love the simplicity of of my markup using these directives. I have an issue I’m wondering if you can help with:

I’m using the @image directive inside a blade template for an ACF block (using mwdelaney/sage-acf-gutenberg-blocks). I’m passing an ACF field name as the first argument ie. @image( 'my_image_field', 'large', ... ). Strangely, when I update the value of this field in the block editor, for example swapping an image, the view isn’t being re-compiled and I continue to see the cached view (with the old field value) when I refresh the page. After running the WP CLI command wp acorn view:clear the view is updated and displays correctly. This issue does not occur if instead of passing the field name as the first argument, I pass the value itself ie. @image( get_field( 'my_image_field', 'large', ... ).

I don’t know much about how views are cached in Sage and I’m struggling to find much information on it, so apologies if this isn’t actually anything to do with your code, but thought perhaps you might be able to shed some light.

Thanks a lot,

Did you find how to make it work? Still giving me the same output.