Laravel Blade
Blade is the templating engine that was created by and included with Laravel. Blade has many capabilities which makes creating view templates fast and powerful. Blade templates can contain PHP tags, and will, in fact, be compiled down to plain PHP.
Display Data
We can display data in Blade using the double curly braces .
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
You are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function.
<html>
<body>
<h1>Hello, {{ $name }}</h1>
<p>The current time is {{ date("H:i") }}</p>
</body>
</html>
Blade statements are 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.
<html>
<body>
<h1>Hello, {!! $name !!}.</h1>
</body>
</html>
Control Structures
Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops.
if statements
You may construct if statements using the @if
, @elseif
, @else
, and @endif
directives, which function identically to their PHP counterparts:
<html>
<body>
@if (isset($name))
<h1>Hello, {{ $name }}</h1>
@else
<h1>Hello, Guest</h1>
@endif
</body>
</html>
Loops
Blades provides directive for working with PHP loop structures, which, again, function identically to their PHP counterparts.
<html>
<body>
@for ($i = 0; $i < 10; $i++)
<p>The current value is {{ $i }}</p>
@endfor
<ul>
@foreach ($list as $item)
<li>$item</li>
@endforeach
</ul>
</body>
</html>