How to comment code in blades like laravel 4? - laravel-4

I have migrated my app from laravel 4.2 to laravel 5
I am currently having this problem, when even I have old comments like this:
{{--{{link_to_route('language.select', 'English', array('en'))}}--}}
Will result into error at laravel 5, I will have this error:
FatalErrorException in 18b6386ebc018eb0c0e76f105eba4286 line 263:
syntax error, unexpected '{'
which is compiled into:
<?php echo --{{link_to_route('language.select', 'English', array('en')); ?>--}}
I already added laravel 4 backward comparability support at register#ServiceProvider as:
\Blade::setRawTags('{{', '}}');
\Blade::setContentTags('{{{', '}}}');
\Blade::setEscapedContentTags('{{{', '}}}');
but how can I add laravel 4 backward comparability for comments {{-- --}} ?
edit:
how to comment this in laravel 5:
<li {{ (Request::is('/') ? ' class="active"' : '') }}>{{trans('messages.Home')}}</li>

Since you change your content tags from {{ to {{{ comment tags are now {{{-- not {{--

From lavarel 5 doc
Note: Be very careful when echoing content that is supplied by users
of your application. Always use the double curly brace syntax to
escape any HTML entities in the content.
{{-- This comment will not be in the rendered HTML --}}
So I think this should works :
<li {{-- (Request::is('/') ? ' class="active"' : '') --}}>
{{--trans('messages.Home')--}}
</li>
And to comment the whole HTML add :
{{{-- HTML --}}}

In general the comment syntax has not changed in Laravel 5, however...
The characters for comments are derived by the content tags. Since you set them to {{{ and }}} with Blade::setContentTags('{{{', '}}}'); you have to use them now for your comments as well:
{{{-- {{link_to_route('language.select', 'English', array('en'))}} --}}}

Related

Laravel Blade: What is nesting sections doing here?

I don't know what does putting a section inside another section do. In a Laravel project I'm reading, a Blade file's code is like this:
login.blade.php :
#extends('layout') #section('content')
#section('title', 'Log in')
Lots of content.
#endsection
In above, what's the point of having title section inside content section? How will it be different if title section is placed outside:
#extends('layout')
#section('title', 'Log in')
#section('content')
Lots of content.
#endsection
I tested, and both are producing same output (HTML source code).
layout.blade.php
<head><title>#yield('title')</title></head>
<body>#yield('content')</body>
Is there any case of layout.blade.php in which different outputs will be produced?
The section directive simply copies whatever you have within #section and #endsection to the name #yield place holder on the extended template.
This means it does not consider where its placed, but for clarity and readability, the second example is the right structure.
For example, this shows that the order of the directive doesn't matter
In welcome.blade.php
#extends('default')
#section('a')
This is a
#section('c', 'This is c')
#section('b')
This is b
#section('d', 'This is d')
#endsection
#endsection
In default.blade.php
#yield('c')
#yield('a')
#yield('b')
#yield('d')
The output
This is c This is a This is b This is d
The best practice is to make your code more readable by opening and closing each block:
#extends('default')
#section('a')
This is a
#endsection
#section('c', 'This is c')
#section('b')
This is b
#endsection
#section('d', 'This is d')
It's better to put them on different rows for readability sake.
First section just puts "Log in" wherever the section "title" is:
#section('title', 'Log in')
Second section has the beginning and the end and is selfdescriptive.

How to render HTML when inside {{}} on Brade with ternary operator in Laravel 5.7?

I have the follow code in Blade using a ternary operator:
<td>{{isset($arrTemp[$ccc->id]) ? "<a hfet='".url('/cc/'.$cc->id)."'>".count($arrTemp[$cc->id])."</a>": 'N/A'}}</td>
If it find somenthing for the array key $cc->id, should thisplay the value with the link atteched to it.
But the page is rendering <a hfet='http://my.test/cc/56526235'>4</a> the string itself.
What am I missing?
When you use {{ }} the output is automatically escaped to prevent XSS attacks. You can use {!! !!} instead, which will not escape the string.
Source: https://laravel.com/docs/5.4/blade#displaying-data

Laravel blade template once echoing variable and once not error

I recently found strange behavior of blade in Laravel 5.8.
Let's say I have in file
#extends('layout')
#section('title','Dodaj playlistę')
#section('content')
{{$hosts = \App\Host::all()}}
<h2>Dodaj audycję do bazy</h2>
in the middle of the file I have
{{$hosts = \App\Host::all()}}
#foreach ($hosts as $man)
<option value='{{$man->id}}'>{{$man->name}} </option>
#endforeach
The problem is I always get the first $hosts variable echoed, while second not. What the hell? Such variable shouldn't be echoed at all because the command is only variable value attribution.
I've checked all my routes, there is no dd() or var_dump() command anyhere.
Looks like possibly a typo pushing for two sections within one. It should normally throw an error, but might be getting confused with the different language.
Change #section('title','Dodaj playlistę') to #section('title') and give it a try.
Also, set the var directly in the foreach for better clarity:
#foreach ($hosts as $man)
Becomes
#foreach(\App\Host::all() as $man)
{{$hosts = \App\Host::all()}} that will echo "\App\Host::all()"
you can
#php
$hosts = \App\Host::all()
#endphp
but it is discouraged to do in a view
see here How to Set Variables in a Laravel Blade Template

Laravel blade syntax for #if/#endif spacing issueif

what is the similar blade syntax for the following code?
<?php if(...):?>abc<?php else:?>cde<?php endif;?>
the code
#if(...) abc #else .... is no good as it ads a space before and after the "abc" the code #if(...)abc#endif (no spacing before and after html string) generates error ...
Thanks
Solution
The correct solution for this problem would be following:
#if(1==1){{ '1' }}#endif
This happens often and makes problem with "space sensitive" parts of code like values in <option> tags.
Having the #if(1==1) 1 #endif would be compiled to following which shows empty spaces around the number:
<?php if(1==1): ?> 1 <?php endif; ?>
Solution code would be compiled to this code:
<?php if(1==1): ?><?php echo e('1'); ?><?php endif; ?>
Which pretty much explains why this won't make additional spaces.
Did a bit more research and it looks like there is no solution as there is no spaceless tag in blade. I found a solution from someone who wrapping his string in extra hml tags (so that is easy as he ads spaces before and after the tag and the string inside tag si spaceless) but in my case I just need a string no spaces before and after ... I will go the old fashion php way ...
Try this:
#if(...) {{ 'abc' }} #else
I've run into similar problems with spaces.
A workaround I use is this:
Put your if statement into php tags
#php
$myVar = 'abc';
if(...) $myVar = 'cde';
#endphp
and then echo the defined variable
{{$myVar}}
It's a workaround, but I still think we always should remember that it's a PHP environment...
So, simply:
#php
if(...) echo "abc";
else echo "cde";
#endphp
I have found the solution in case there is space issue between #if()#endif and #if.
I have replaced the #if() #endif with the {{$wardname}} variable to be printed using {{$wardname}}#if and its removed conflict with #endif & #if
and applied logic like:
if($city->wardname!="") {
$wardname = "(".$city->wardname.")";
}else{
$wardname = "";
}
Implemented it as:
{{$wardname}}#if
The correct syntax is:
#if(...)
abc
#else
cde
#endif
I never saw unwanted spaces with this one.

Blade template not executing #else clause

I have a Blade template that loads a list of users and displays their various details.
If a user has no mobile number I want to display the message "No Mobile Number" (I've tried single and double quotes), this never gets displayed:
#if ($person->Mobile >= "")
{{ $person->Mobile }}
#else
'No Mobile Number'
#endif
I tried substituting the "No Mobile" message with {{ $person->EMail }} (which I'm displaying elsewhere, so I know everyone has an email address), but still go nothing, as far as I can tell the logic isn't going into the #else block.
Any ideas?
This should work
#if (!empty($person->Mobile))
{{{ $person->Mobile }}}
#else
'No Mobile Number'
#endif
I use this approach:
#if( isset($error_message) )
#section('content')
#parent
#stop
#section('error_message')
<strong>Holly molly! </strong><em>{{ $error_message }} :(</em>
#stop
#endif
I have a section content and inside content have another section error_message so if error_message variable is set, show that content section and inside print my error_message.
PD: I haven't my original code to hand... Im currently using Twig as primary template engine in Laravel4. Twig beats Blade in simplycity and is very usefull

Resources