Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blade template vs plain php in Laravel

Tags:

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?

like image 532
castt Avatar asked Mar 14 '14 22:03

castt


People also ask

What is difference between blade PHP and PHP?

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.

What is the advantage of Laravel blade template?

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.

Why does Laravel use the blade template engine?

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 .

Can I use blade template without Laravel?

It's 100% compatible without the Laravel's own features (extensions).


1 Answers

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.

Speed

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.

like image 192
Jason Lewis Avatar answered Sep 25 '22 10:09

Jason Lewis