Laravel Blade

Bladeopen in new window is a simple but powerful templating engine included with Laravel. Blade has a collection of directives, which makes templates easier to read and create. It also has a sophisticated component system used to modularize a web application and build template layouts. Finally, unlike other templating engines, Blade uses plain PHP code inside the templates and is converted to plain PHP when the template is compiled. This means Blade is simpler and more powerful than plain PHP and yet adds zero overhead to a web application.

Blade template files use the .blade.php extension and are typically stored in the resources/views directory.

Rendering Blade Files

Like other view files, Routes can display Blade view files using the view helper function.

Route::get('/greetings', function () {
  // renders /resources/views/greetings.blade.php
  return view('greetings'); 
});

NOTE

Remember that only the base name is required. Laravel will know to look for .blade.php.

Data can also be passed to a Blade view file by adding an array to the view helper function.

Route::get('/greetings', function () {
  return view('greetings', ['name' => 'John']);
});

Display Data

Displaying Dataopen in new window in a Blade file can be accomplished by wrapping the variable in double curly braces {{ }}. This is sometimes known as moustache syntax.

Hello, {{ $name }}.

The Blade {{ }} statement is automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. However, if you do need to display raw HTML, it can be done using the {!! !!} statement.

Hello, {!! $name !!}

Sometimes, it may be necessary to prevent the Blade engine from rendering the moustache syntax. For example, if you were also working with the Vueopen in new window framework. In this case, the @ can be placed in front of the {{ }}. When the Blade template is rendered, the @ will be removed, but the {{ }} will remain.

Hello, @{{ name }}. // rendered as: Hello {{ name }}