As already stated in some of the comments, Blade does not prevent the user from using php tags.
However, there are those who still prefer a solution in the Blade
- like me.
Way 1 - The gambiarra
So the first option is to do a little gambiarra.
The Blade
compilation {{ $valor }}
for <?php echo $valor; ?>
- I’ve already opened the source code and I know it’s like this.
The solution would be to do this:
{{ ''; $valor = 1 }}
That would be compiled for this:
<?php echo ''; $valor = 1; ?>
It would still be possible to use the same gambiarra with the comments tag from Blade
.
{{-- */ $valor = 1; /* --}}
The exit would be:
Way 2 - Extend Blade
I prefer this second form, because a gambiarra always generates confusion and problems, in the future.
We can extend the Blade
and add a new syntax to your compiler. This is done through the method Blade::extend(Closure $closure)
.
For example, let’s define that expression {? $valor = 1 ?}
be interpreted by Blade
as <?php $valor = 1; ?>
.
Just add the following statement to the file app/start/global.php
:
Blade::extend(function($value) {
return preg_replace('/\{\?(.+)\?\}/', '<?php ${1} ?>', $value);
});
There is no way, but Blade does not restrict the use of php in your views. so if you use <?php $value = 1 ? > {{ $value }} will print 1
– rzani
"There is no way" is a very general statement. Have as it has, because Blade offers mechanism for you to create your own syntax, besides having a small workaround for it.
– Wallace Maxters
What would be the use of this statement? There really is no way to pass this variable by reference?
– rzani