<?php vs. @php tags in Sage 10.beta-2

Hi Sage-experts

I am trying to make a theme with ACF and Sage 10-beta.2

For some reason, I have some troubles with setting a variable from ACF when I’m using the Blade-syntax. It took me some time to figure out that if I use <?php instead of @php, everything worked.

This works:

<?php 
$myVar = get_field('link');
?>
{{ $myVar }}

Dosen’t work:

@php
$myVar2 = get_field('link');
@endphp
{{ $myVar2 }}

Am I doing something wrong?

Howdy! Some questions:

What does “doesn’t work” mean? Is there an error? Is there no output?

Is there a reason why you’re trying to define a variable in the view instead of defining it from a view composer? Regardless, I’m not sure why the php directive isn’t working as expected for you, this works fine for me:

@php
$hello = 'world';
@endphp
{{ $hello }}

If this is just a snippet from the whole doc, there may be a chance you’ve used the @php( ... ) shorthand for @php and @endphp if I recall correctly you cannot mix these forms, you must always use @php() or always @php and @endphp

You can check your compiled view for confirmation of what was produced.

But I also would highly recommend using view composers like @ben hinted at. Here is a doc on how they work Working With View Composers in Sage 10 | Roots

1 Like

omg. You were right! I just discovered that there was a @while (have_posts()) @php(the_post())
I am sorry for posting this issue. Thank you for pointing me in the right direction.

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