Blade template engine

Calling the template

The template engine compiles files with the extension blade.php in the php code. The compiled files are located in the /temp/blade folder. In debug mode, the files are compiled each time they are run, and in production mode (debug = false), the files are compiled once. To recompile them, you need to manually delete them from the /temp/blade folder.

The template name is set without an extension blade.php. If the template is located in a subdirectory, then the name of the subdirectory is specified on the left, then the dot, then the template name. The nesting levels of subdirectories are not limited. For example, if the template is located on the path:


that template name is 'books.petrov.history'. Since the blade template engine if the working directory /view/default is set, the template name is formed relative to the working directory.

Call example:

view()->show('main.index', [
    'var1' => 'Value 1',
    'var2' => $value2

The template is called to the screen by the view()->show($name, $vars = []) method an object of the Aleksmir\Core\View\View class. Here, $name is the template name, $vars - variables that can be used in the template.

If you want to call the template without specifying the folder name language , then to call the template, use the route()->view($name, $vars = []) method of the \Aleksmir\Core\Route\Route object. Example of calling a template with a language folder:


Example of calling a template without specifying the language folder:


In the first case, the language folder /lang/en and the template are specified index.blade.php it will be opened from this folder. In the second case, the template will be opened from the folder corresponding to the current language. That is, if the main language is Russian, then when you select English,'.lang.en'is automatically substituted in the template name, and when you select the Russian language, the template name will remain unchanged.

Basic Directives

Inside the template, the values of variables are output in double curly brackets, for example:

<div> {{ $var1 }} </div> , will be output <div> Value1 </div>

Inside {{ } } you can use the PHP language functions that are available globally. By default, all content between {{ and}} is handled by the htmlentities() function to prevent XSS attacks. There are situations when the text must be transmitted to the browser in a pure raw form. To do this, you can use the construction {!! ... !!} or @ {{ ... }}. For a large section of code, the following construction is used:

...some code

Templates can be nested within each other. It is more convenient to call the child template itself, and it will load the parent template. The number of parent levels is unlimited. Command to load the parent template:


Here 'parent' is the name of the parent template.

You can insert a section from a child template into a parent template, and specify the following in the parent template:


Here 'sect_name' is the name of the section. In the child template, you need to create the content of the section:

... content of the child template section

If the parent template explicitly specifies a section:

...contents of the parent template section

then in the child template, you can use the directive to insert this content and even add it its new content. Example of a child template section:

...content of the child template

Another operator for inserting another template: @include($name, $var = []) ,
here $name is the name of the template to insert, $var is a named array with a list of parameters.


The @if condition directive...@endif :

@if (condition)

The "if not" directive @unless :

@unless (condition)

Abbreviated conditional directive: instead of {{ isset($var) ? $var : 'no value' }} you can use:{{ isset($var) or 'no value' }}

The @hasSection('name') directive allows you to determine if the child template has a 'name' section:



Loop directives @for , @while , @each :

@for ($item = 0; $item < 5; $item++)
{{ $item }}

@foreach($pages as $page)
Page «{{ $page->title }}»

@forelse($lines as $line)
String {{ $line }}
The list is empty

@while (condition)

@each('parent', $array, 'var_name', ['parent_empty'])

Here 'parent' is the name of the pattern that will be repeated for each element of the $array array, in this case, the template will be substituted with the value of the current element, available from the template in a variable named 'var_name'. The 'parent_empty' template is optional, it will be displayed if the $array is empty.

Inside the loop, the @continue ([condition]) - skips the current iteration, @break([condition]) - interrupts the loop. The condition in these directives is optional. If the condition is not set, the skip or interrupt occurs without conditions.

The $loop variable is also available inside the loop:

$loop->index - index of the current iteration (starts with 0)
$loop->iteration - current iteration (starts with 1)
$loop->remaining - the number of remaining iterations of the loop
$loop->count - total number of iterations
$loop->first - flag for the first iteration
$loop->last - indicates the last iteration
$loop->depth - nesting level of the current loop
$loop->parent - the $loop variable of the parent loop

Other useful directives

Comments in the blade template:
{{-- This comment will not be displayed in the final HTML --}}

The @php directive allows you to insert any arbitrary section of PHP code into a template:

...some PHP code

Named stacks . In the template, you can insert code into a named stack, for example:

<script src="/script.js"></script>

In another template, you can display this stack:


Creating custom directives

You can create an object of the eftec\bladeone\BladeOne class using the directive method:

view()->blade->directive('name', function($param) {
    ... function code

Here 'name' is the name of the directive, $param is the input parameters of the directive.