Summary of Custom Template Instructions in Laravel Framework

  • 2021-08-28 19:34:34
  • OfStack

Introduction

Recently, when learning laravel, one way to use templates in Laravel framework is through controller layout. Usually, we write complex logic in view templates, which looks messy. Then using custom template Directives can simplify your view logic and write more elegant code. Laravel Blade is a template engine that compiles its special syntax into PHP and HTML. Its special grammatical instruction, which is sugar-adding function, hides messy code after it. Templates contain a lot of built-in directives, such as @ foreach/@ if/@ section/@ extends, etc. The built-in directives are enough for a simple project, but when you write repeated and complex functions in your code, custom template directives may help you optimize your view structure.

The following words are not much to say, let's take a look at the detailed introduction.

Customize 1 Simple Template Directive

The $expression parameter is optional


\Blade::directive('directive_name', function ($expression) {
 return $expression;
});

Usage in view Demo


<p>@hello('World')</p>

Declare the location of custom template instructions AppServiceProvider. php


<?php
namespace App\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
 /**
  * Perform post-registration booting of services.
  *
  * @return void
  */
 public function boot()
 {
  Blade::directive('hello', function ($expression) {
   return "<?php echo 'Hello ' . {$expression}; ?>";
  });
 }
 /**
  * Register bindings in the container.
  *
  * @return void
  */
 public function register()
 {
  //
 }
}

Instructions defined in this way load successfully and can be used in any template

Matters needing attention

Point 1

You can't directly access multiple parameters passed in custom instructions, so you need to traverse them


<p>@greet('Hi', 'Hammad')</p>
\Blade::directive('hello', function ($expression) {
 list($greet, $name) = explode(', ', $expression);

 return "<?php echo {$greet} . ' ' . {$name}; ?>";
});

array () list () is not a function, but a language structure

Point 2

Always remember that you need to filter the output. When you use {{}}, Blade has already executed the filtering operation in advance. In order to avoid malicious users injecting js code into the site, you must escape HTML. You can use the function e () that comes with Laravel, which is also equivalent to htmlentities ()


\Blade::directive('hello', function ($expression) {
 return "<?php echo 'Hello ' . e({$expression}); ?>";
});

Point 3

Every time you add or modify a custom template directive, you must clear the cached view template first. You can use clear Artisan


php artisan view:clear

New features of Laravel5.5

When using custom template instructions, most of them are only conditions in some form. These requirements require us to register three independent instructions, if/else/endif. At present, Laravel5.5 already supports simplified condition instructions. For example, the following example, the template can use admin/else/endadmin


public function boot()
{
 \Blade::if('admin', function () {
  return auth()->check() && auth()->user()->isAdmin();
 });
}

Summarize


Related articles: