Hello! Why the simplest syntax does not work
@php
$counter = 1;
$secondCounter = $counter + 22;
@endphp
Does not work ?
Do you have any logs ?
Have you saved your file as .blade.php?
Hi, were you using @php(...)
format as well as the @php ... @endphp
format of the blade directive in this file? Blade only supports using one style in a single file so they all have to be one or the other.
Mixing them can cause the blade templates to partially compile leaving behind some of the directives.
I am still new to Blade, but if I use @php @endphp format in the standard footer.blade.php it indeed gives me errors (see below)
Are those errors predictable?
And why would Sage use @php() as the default format here then?
<footer class="content-info">
@php(dynamic_sidebar('sidebar-footer'))
@php $counter = 1; @endphp
{{ $counter + 1 }}
</footer>
This gives an error ‘Undefined variable $counter’
Commenting out @php(dynamic_sidebar('sidebar-footer'))
, gives error: ’ unexpected token “<”
<footer class="content-info">
@php $counter = 1; @endphp
{{ $counter + 1 }}
@php(dynamic_sidebar('sidebar-footer'))
</footer>
This works fine
Commenting out @php(dynamic_sidebar('sidebar-footer'))
still works fine
Are you sure you don’t have any faulty PHP in your sidebar scripts?
Yes. It is default Sage. didn’t touch it. And it works fine in the second example. Is this not reproducible?
Your example in the footer is mixing the formats of @php
Just don’t do that.
If you need to add a block @php
like you have, you need to also go and update all inline @php()
uses to be blocks.
And why would Sage use @php() as the default format here then?
They would and have done this because adding logic into the view, through the use of @php
is contrary to MVC and they tend to only dip into PHP for small single line calls that are easier to do quickly instead of creating a sage directive for the one or two places they need to do it.
Just don’t mix @php( )
and @php @endphp
in your files and you will be good.
I figured that. I find it really counter-intuitive, but maybe there is a reason for it.
Thanks for explaining. Still learning how you are ‘supposed’ to do things in Sage 10.
The fact that you can’t mix and match the formats is definitely confusing. Perhaps a bug. They don’t mention the inline directive format anymore so maybe they don’t really support it. But this tripped me up too so I’ve been watching for others with mixed formatting to help save some frustration.
The best practice of having logic handled outside of the view in controllers or composers is one from Laravel and Model View Controller (MVC). And thus a part of Sage.
The Laravel Blade Docs used to have a note about how although @php
is supported by blade, the use of it might indicate you are doing things wrong. They seem to have since removed that.
While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template.
In fact there isn’t anything preventing you from just inserting a traditional PHP block <?php ?>
other than consistency.
@EHLOVader is correct, you must use either @php()
or @php @endphp
- you can not mix them.
I’m to blame for the use of @php()
instead of @php @endphp
in Sage’s default templating. My reasoning is/was:
- We have to do a ton of 1-liners for WordPress stuff.
- Composer’s provide an extremely powerful way in Sage/Acorn to handle all of your logic. Not even Laravel out of the box has the raw power of Acorn’s composer implementation without implementing it yourself. You can attach to literally every view on the fly.
I’ve written massive sites on Sage 10 at this point and have NEVER reached for @php
in my views. The only time I could see it necessary is if you were bringing over another plugin’s view with a ton of logic inside of it, and in that case, you can simply use @php @endphp
as needed (or even keep it raw PHP).
This thread definitely made some things click for me. Thank you.
I know there is a shortage of time, but maybe it is an an idea to make an example theme available at some point? I imagine there is quite some good source code lying around. A homepage with some sections, a few blocks, restyled core blocks, some usage of theme.json (and how to style your editor), the use of tailwind.config.js, an external js library such as Alpine etc. In short a theme that integrates most things you will encounter all the time.
I already own the Sage book by the way (and should probably read it some more). But I know I learn a lot from looking at real code, and I think it would be a great head start for people who have fallen behind on modern Sage (or modern web development in general).
I would happily pay for it too.
It would be really helpful: I agree with you @cim
I am very grateful to ra detailed answers. Indeed I mixed @php @endphp and @php()
But in some cases it worked fine for me, for example:
@php
$sectionTitle = __('Section title example', 'sage');
$docListSubTitle = __('Column title example', 'sage');
@endphp
......
@hasfields('alimenti_list')
<div class="col-md-6">
<h3 class="text-center ba-doclist__subtitle">{{$docListSubTitle}}</h3>
<ul class="ba-doclist__list">
@fields('alimenti_list')
<li class="shadow-sm">
<h5 class="ba-doclist__list-subtitle">
<a href="@sub('link')">@sub('title')</a>
</h5>
</li>
@endfields
</ul>
</div>
@endhasfields
.....
<ul class="ba-doclist__list">
<?php while( have_rows('divorce_list') ): the_row(); ?>
<li class="shadow-sm">
<h5 class="ba-doclist__list-subtitle">
<a href="@php(the_sub_field('link'))">@php(the_sub_field('title'))</a>
</h5>
</li>
<?php endwhile; ?>
</ul>
don’t pay attention to different ways of writing code, I’m still learning the syntax Sage, and trying to figure out when and what kind of errors might occur