Summary of custom template directives in the Laravel framework

Source: Internet
Author: User
Blade allows you to customize the command, you can use the directive method to register the command. The following article mainly give you a summary of the Laravel framework of the custom template directives related materials, the text through the sample code introduced in very detailed, the need for friends can refer to, let's see together

Introduced

One of the ways to use templates in the Laravel framework recently in learning Laravel is through controller layout, where we often write complex logic in a view template and look cluttered, so using a custom template directives can simplify your view logic, Writing more elegant code, Laravel blade is a template engine that compiles its special syntax into PHP and HTML. Its special syntax instruction is the sugar-sweetened function that hides the messy code behind it. The template contains a number of built-in directives, such as @foreach/@if/@section/@extends, etc., and the built-in directives are sufficient for a simple project, but when you write repetitive complex functions in your code, custom template directives may help you optimize your view structure.

The following words do not say much, come together to see the detailed introduction.

Customizing a simple template directive

$expression parameter is optional

\blade::d irective (' Directive_name ', function ($expression) {return $expression;});

Use demo in view

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

Declaring the location of a custom template directive appserviceprovider.php

<?phpnamespace 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::d irective (' Hello ', function ($expression) {   return ') <?php echo ' Hello '. {$expression};?> ";  }); }/**  * Register bindings in the container.  *  * @return void *  /Public Function register () {  //}}

Instructions defined in this manner are loaded successfully and can be used in any template

Precautions

1th

You cannot directly access multiple parameters passed in a custom directive and need to traverse them out

<p> @greet (' Hi ', ' Hammad ') </p>\blade::d irective (' Hello ', function ($expression) {list ($greet, $name) = Explode (', ', $expression); Return "<?php echo {$greet}." ' . {$name};?> ";});

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

2nd

Be sure to always remember the need to filter the output, the general use of {{}}, Blade has been pre-implemented filtering operations, in order to avoid malicious users to inject JS code into the site, be sure to escape HTML, you can use the Laravel function e (), also equivalent to Htmlentities ()

\blade::d irective (' Hello ', function ($expression) {return "<?php echo ' Hello '. E ({$expression});?> ";});

3rd

Every time you add or modify a custom template directive, be sure to clear the cached view template before you can use the clear Artisan

PHP Artisan View:clear

Laravel5.5 new Features

When using custom template directives, most are just some form of condition, which requires us to register three independent instructions, IF/ELSE/ENDIF, currently Laravel5.5 has supported simplified conditional directives, such as the following example, templates can use admin/else/ Endadmin

Public Function boot () {\blade::if (' admin ', function () {  return auth ()->check () && auth ()->user ()- >isadmin (); });}

Summarize

Articles you may be interested in:

PHP recursive implementation of a quick sort of method Example explained

A tutorial on how to implement GIT deployment in PHP

An example analysis of the binary search algorithm implemented by PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.