I am moving from cakephp to laravel and I would like to understand what is the advantage of {{ }}
over the short php tags <?= ?>
. I would guess the php tags are faster, not needing any processing by the template framework. I understood that {{ }}
will do some escaping but when not necessary why using {{ }}
and not <?= ?>
. Also what is the advantage of @foreach
and @if
... vs <?php foreach():?>
... <?php endforeach ?>
and <php if():?>
<?php endif;?>
.
from Laravel docs:
Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
Additionally if you are using blade template engine, it's like convention to use {{ }}
{{ ... }}
works only with blade files while <?= ... ?>
works with every php file. This is the only difference I know.
The advantage of {{ }}
is its look pretty easy to understand also escape the special character from the string. After using @foreach
we can minimize code and its also simplify the code to under stand
Thank you all for your answers but I still did not get the answer I was looking for ... Meaning a clarification on what path should I go with Laravel (as I come from years of cakephp and things were simpler there in terms of templating). I understood and mentioned that {{}} is escaping strings for XSS, clear until now ... but my problems are as follows:
I am migrating some templates from cakephp and is already there ... no i have to move eveything to {{}} and @foreach and @if
Isn't this {{}} @foreach tags adding an overhead and extending the execution time? I mean the template has to conver all these tags to php tags in the end, right ?
I did not find a GOOD editor (or IDE) to nicely display these tags, to show me the beginning and end of repetitive or decision structures, to color and highlight the syntax inside the tags ... so It's kind of annoying ... at least using i see VERY clear in all editors if i closed the brackets, arrays if it's i am missing a ' or " ... etc. Any suggestion of a good editor to recognize the blade syntax?
As I understand the benefit of {{}} (escaping xss), but what is the benefit of @foreach vs or the @if vs ???
As I dont know laravel yet (and all it's power) is there a hidden feature that I am not aware of that will make it hard to work (migrate, extrapolate, color, add smell :))) joking) ... in the future if i use php tags instead of blade tags in my files? For example if o use blade tags will it be easier in the future to migrate all my files to a different templating engine and it wont work if i use php tags?
Thank you again for sharing your thoughts