{{-- --}}
means it's commented out, so that won't work. Why not pass the variables from the controller or route? Or just use regular php tags...
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello Team, I have upgrade to laravel 5.3, It looks good with more new features. However in blade file the variable assignment is not working same as laravel 5.2
I am using {{--*/ $str = 'Laravel 5.3' /*--}} and use this as {{$str}} . Its giving me an error like Undefined variable: str
Please suggest me a way to overcome this issue.
Thanks in advance.
{{-- --}}
means it's commented out, so that won't work. Why not pass the variables from the controller or route? Or just use regular php tags...
I think this is the way
{{--*/$i=1/*--}}
@Prullenbak I am using the laravel standard which was work in laravel 5.2 such as {{--/ /--}}
@amitshrestha221 Yes I am using the correct way... Not able to mention the proper syntax how to show code differentiate. But the syntax you mentioned is not working in laravel 5.3
Ok. But still...what's wrong with
<?php $str = 'Laravel 5.3'; ?>
{{ $str }}
Isn't that more clear then some weird misuse of comments (because that's basically what you're doing?)
@Prullenbak the sole concept of blade templating is to clear out php tags in view files.. so why use them when we have blade templating formats?
@sanjay23 did u try with * ?? since no one has posted this issue, i guess that's working..
{{--*/$i=1/*--}}
the sole concept of blade templating is to clear out php tags
No, that's not the sole concept. The concept is that you can extend views better, and you can type {{ instead of <?= and a lot of stuff like foreach loops are easier to type, etc etc.
What you're doing now is adding comments that are commenting out. Beacuse laravel will replace {{-- with <?php /* You're basically doing <?php /**/ $=1; /**/?> wich is super weird if you ask me. But to each his own, of course.
Quick question: Do you guys use blade highlighting in your editor?
Because in my editor, a line like
{{--*/ $i = 1 /*--}}
Will be completely greyed out. That's not ideal in my opinion...
If you're doing this (defining variables in blade views) a lot, a better solution might be to extend blade with a @define() method so you could do stuff like @define($i,1) or something like that.
Or, as I suggested earlier, just pass the variable from the controller/route closure that makes the view.
{{--*/ /*--}} I am using the same format
@amitshrestha221 I totally agree with you that no one posted this issue so far as laravel 5.3 launch 2-3 days ago officially.
I have tried the code
Laravel 5.2
{{--*/ $str = 'laravel5.2' /*--}} and access as {{ $str }} It's working fine :)
Laravel 5.3
{{--*/ $str = 'laravel5.3' /*--}} and access as {{ $str }} It's not working :(
As I said, {{--*/ $str = 'laravel5.3' /*--}} will be replaced with <?php /**/ $str = 'laravel5.3' /**/ ?> So you might want to add a semicolon after your declaration:
{{--*/ $str = 'laravel5.3'; /*--}}
@Prullenbak I am using this code.. where is ; there?? this is fully functional code..
<tbody>
{{--*/$i=1/*--}}
@forelse($cms_data as $cms)
<tr>
<td>{{$i++}}</td>
<td>{{ $cms->title }}</td>
<td>{!! Helpers::string_limit($cms->description,50) !!}</td>
<td>
@if($cms->
@amitshrestha221 Is this work with LARAVEL 5.3 ? Can you please confirm about this.
If you're using 5.3 and it's only for the index of your loops, then you can omit the $i variable altogether, because you can use the new $loop variable
@forelse($cms_data as $cms)
<tr>
<td>{{$loop->iteration}}</td>
But for other variables, the comment approach should work. But I'm still curious why
{{--*/$i=1/*--}}
looks better to you then
<?php $i = 1; ?>
@sanjay23 i havent tried 5.3 yet.. but i guess it will work.. If it hadn't worked, laracasts wud have been full of this error.. :D
If it hadn't worked, laracasts wud have been full of this error.. :D
Nah, my guess would be not that many people are doing this that way...
@amitshrestha221 @Prullenbak :D , Unfortunately it's not working in my case, Really don't know why this is happening.
Well, back to the basics then:
@sanjay23 go with.. $loop->iteration
@amitshrestha221 thanks but I am not using in any iteration. Its simply assignment of variable that need to be print in view.
Then continue with question 2 in my list above ;)
-Is it nessecary to define the variable in the view? Or can you pass it from somewhere else? -If not: Do you need to do this often? Make (or find) a blade directive to define variables -If not: So it's a one time thing, is it really that big of a deal to just use a couple of php tags?
@Prullenbak yes its sometimes necessary, according to requirements.. however why to use php tags when blade templating has made it easier??
Again:
Do you need to do this often? Make (or find) a blade directive to define variables
If you don't want to do that then tell me: Is
{{--*/$i=1/*--}} really easier and better than <?php $i = 1; ?>? And why?
Also, I'm curious what those "requirements" are.
Thanks very much guys for your comments over it. @Prullenbak agree with your comment however if we really want to use the laravel standard method so don't want to use core php tags again even laravel convert the {{}} it in php tag.
I have used the @php $str = 'laravel 5.3' @endphp Its work fine and happy to see the proper laravel code in blade.
It's directive predefined Ref link - http://themsaid.com/laravel-5-2-23-20160305/
O. That would've saved us a lot of replies to this thread :P
You could make it even nicer according to that:
@php($str = 'laravel 5.3')
Yes it is, Actually I have a block of code so I used loop. But really thank you very much to both of you. Really appreciated your kind support.
Thank you
Please or to participate in this conversation.