As I understand, Blade
is simply regex
parser, which will translate any Blade
construction into PHP
code, and then generate plain HTML
from that PHP
. Seems like this process makes loading files with Blade
templates slower (because of the extra step Blade
-> PHP
). If so, why do I want to use Blade
at all? Just because of the elegant syntax or because Blade
files are stored in cache
?
Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. All Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.
Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .
It's 100% compatible without the Laravel's own features (extensions).
You'd use Blade because you want to use Blade. Like you've said it has a much nicer syntax and once you know its simple syntax it's very quick to use.
Regular PHP:
<?php if ($user->isLogged()): ?>
Welcome back, <strong><?= $user->name; ?></strong>
<?php endif; ?>
Blade:
@if ($user->isLogged())
Welcome back, <strong>{{ $user->name }}</strong>
@endif
Of course that's just a basic control structure. Blade has built in templating support as well.
There should be virtually no speed difference between the two as on the first load Laravel will compile any views that have changed into their PHP equivalent. Subsequent page loads will use this compiled file instead (they are stored at app/storage/views
).
I guess the only extra overhead would be the initial check to see if the view has been compiled yet. Bugger all though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With