Laravel Blade template component for Kirby CMS.
Blade is the simple, yet powerful templating engine provided by Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your site.
Blade view files use the .blade.php
file extension and are typically stored in the site/templates
directory.
This component extends Kirby’s built-in templating engine. Any regular .php
template will continue to works as usual.
Basic Usage
Blade Templates works out of the box without requiring any configuration. You just need to add .blade.php
files to site/templates
.
Defining a Layout
Blade uses the concept of template inheritance and sections. You can define master layouts which are extended by child views. Check out how a master layout would look like in Kirby:
<!-- Stored in site/templates/layouts/master.blade.php -->
<html>
<head>
<title>@yield('title') | {{ $site->title() }}</title>
</head>
<body>
<h1>@yield('title')</h1>
<div class="sidebar">
@section('sidebar')
<h2>Description:</h2>
@show
</div>
<div class="container">
@yield('content')
</div>
</body>
</html>
As you can see, this file contains typical HTML markup. However, take note of the @section
and @yield
directives. The @section
directive, as the name implies, defines a section of content, while the @yield
directive is used to display the contents of a given section.
Extending a Layout
When defining a child view, use the Blade @extends
directive to specify which layout the child view should “inherit”. Views which extend a Blade layout may inject content into the layout’s sections using @section
directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield
:
<!-- Stored in site/templates/default.blade.php -->
@extends('layouts.master')
@section('title', $page->title())
@section('sidebar')
@parent
{{ $page->description()->kirbytext() }}
@endsection
@section('content')
{{ $page->text()->kirbytext() }}
@endsection
Show rendered HTML
<html>
<head>
<title>Services | Company Name</title>
</head>
<body>
<h1>Services</h1>
<div class="sidebar">
<h2>Description:</h2>
<p>Service description</p>
</div>
<div class="container">
<p>Sum inusa commolu ptatent mossend elignam volenim quiam quiaspe riaessenis plisita ecaboribus.</p>
<p>Ecaborenis molupta spiene recepudam, quostium reprem rereprat.</p>
</div>
</body>
</html>
Displaying Data
You may display data passed to your Blade views by wrapping the variable in curly braces.
{{ $page->title() }}
You don’t have use the html()
method to escape special characters because Blade’s {{ }}
statements are automatically sent through Kirby’s html()
helper function to prevent XSS attacks. The example above is the same as:
<?php echo html($page->title()) ?>
If you do not want your data to be escaped, you may use the following syntax:
{!! snippet('posts', $posts, true) !!}
While this example shows how you can use a regular snippet with Blade, take note that Blade’s @include
directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view as well.
Check out the plugin on Github!