Laravel blade syntax for #if/#endif spacing issueif - laravel

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.

Related

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

how to define a variable in laravel blade

How can define a variable in the view side(blade) in laravel?
I found that I can do it in this way:
<?php $var = 'something' ?>
But is there any way to do this like {{ $var = 'something' }} or #var1 = 'something' ?(ofcourse without printing it)
I agree with #Kiril Ivanov answer, but if you still want to do that you can use
#php ($variable = 'test')
Thanks
no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements
yes there is a way to do this
first assign your variable like this
{{ $yourvariable='' }}
and after than u can manipulate the variable
#if ($abc['type']=='youresult')
{{ $yourvariable='success'}}
#endif
Hpe this works

How to comment code in blades like 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'))}} --}}}

Smarty: cannot recognize continue tag

all im trying to do is to use the simple continue tag but it keeps giving me error like this:
string(145) "Smarty error: [in module_db_tpl:onlyimage4;image_detail line 26]: syntax error: unrecognized tag 'continue' (Smarty_Compiler.class.php, line 590)"
my code is as follow:
{foreach from=$itemlist item="item"}
< .. SOME CODE ..>
{if $maxCol == $colm}
</div>
{assign var ='colm' value = 0}
{$row++}
{continue} **<- THIS IS THE PROBLEM**
{/if}
<.. SOME CODE ..>
{/foreach}
does anyone have any idea whats wrong, I've been googling and see no comments of such sort everyone seem to suggest that this should work.. any ideas guys...
Old question, but you need to use: {$continue} (including the $)
For smarty 2:
I don't think the tag exists. if you read this thread you can see that there are people that want it, and a suggestion to fix it like so. (have not tried)
compiler.continue­.php
<?php
function smarty_compiler_con­tinue($contents, &$smarty)
{
return 'continue;';
}
?>
(Bold part my addition)
Create these two files (in this case just one) and put them into your plugins directory
(notice the naming convention compiler.xxx.php).
The good news is, for smarty 3 there is such a tag! see the manual, with example:
{$data = [1,2,3,4,5]}
{foreach $data as $value}
{if $value == 3}
{* skip this iteration *}
{continue}
{/if}
{$value}
{/foreach}
{*
prints: 1 2 4 5
*}

How can I create a simple internationalization (i18n) example in Kohana v3.0.6.2?

I've been trying to make i18n work: Just to display a string with 2 different languages. I followed this tutorial, but I think the author is not using Kohana 3.
It basically consist on creating a langauge file (pl.php) file in the i18n folder, and add the following:
<?php
$translations['Good Morning'] = 'Magandang Umaga';
Then changing the locale to pl.
and finally the output in the view file:
<?php echo __('Good Morning'); // would produce 'Good Morning' ?>
I really got lost in the tutorial.
Can anyone give me a small example of using internationalization (i18n) in Kohana v3.0.6.2?
Thanks in advance!
this is how your i18n/pl.php file should look like:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'Good Morning' => 'Magandang Umaga',
);
you can't copy everything that you see from a ko2 tutorial
I think you need to use the key from the translations file. i.e.
<?php echo __('good_morning'); // would produce 'Magandang Umaga' if the locale was 'pl' ?>
but you can also use real words as the key, so in your pl.php file you would have:
$translations['Good Morning'] = 'Magandang Umaga';
and then you can use:
<?php echo __('Good Morning'); // would produce 'Magandang Umaga' if the locale was 'pl' ?>
in your view.

Resources