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:

/view/default/books/petrov/history.blade.php

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:

view()->view('core.main.lang.en.index');

Example of calling a template without specifying the language folder:

route()->view('core.main.index');

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:

@verbatum
...some code
@endverbatum

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:

@extends('parent')

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:

@yield('sect_name')

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

@section('sect_name')
... content of the child template section
@endsection

If the parent template explicitly specifies a section:

@section('sect_name')
...contents of the parent template section
@endsection

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:

@section('sect_name')
@parent
...content of the child template
@endsection

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.

Conditions

The @if condition directive...@endif :

@if (condition)
...
@elseif
...
@elseif
...
@else
...
@endif

The "if not" directive @unless :

@unless (condition)
...
@endif

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:

@hasSection('sect_name')
...
@else
...
@endif

Cycles

Loop directives @for , @while , @each :

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

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

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

@while (condition)
...
@endwhile

@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:

@php
...some PHP code
@endphp

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

@push('scripts')
<script src="/script.js"></script>
@endpush

In another template, you can display this stack:

<head>
@stack('scripts')
</head>

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.