Laravel template engine blade Some of the labels in section _php tutorial

Source: Internet
Author: User

Laravel template engine Blade The difference of some of the tags in section


This article mainly introduces the Laravel template engine Blade section of some of the differences introduced, this article explains @yield and @section, @show and @stop, @append and @override, the need for friends can refer to the following

The Blade template engine in the Laravel framework works well, but in official documents the introduction of Blade is not detailed, some things are not written, and others are not. For example, you might encounter this problem in use:

1. What is the difference between @yield and @section can be pre-defined and replaceable chunks?
2. @section can end with @show, @stop, @overwrite and @append, and what is the difference between those three?

This paper tries to make a relatively simple but intuitive introduction to these problems.

@yield and @section

First of all, @yield is not extensible, if you want to define the part of the default content to let the child template extension, then the form of @yield ($name, $default) is more convenient, if you do not specify the content of this chunk in the child template, it will display the default content, if defined, The content you define is displayed. Either

In contrast, @section can be both substituted and extended, which is the biggest difference. Like what:

The code is as follows:


{{--Layout.master--}}
@yield (' title ', ' default title ')

@section (' content ')
The default content
@show

The code is as follows:


{{--Home.index--}}
@extends (' Layout.master ')

@section (' title ')
@parent
New title
@stop

@section (' content ')
@parent
Content of the extension
@stop

In the example above, the template defines a chunk with @yield and @section, and then defines the content in the sub-template, because @yield cannot be extended, so even if the @parent does not work, the output is only "new title" and replaces "default title". Therefore, the resulting page can only be "default title" or "new title" and cannot coexist. In the @section definition section, the content of the parent template is retained and then added after the extension, because the @parent keyword is used, and the output is "content of the default content extension".

The document on the official web site does not involve @parent keywords, saying that the default behavior is "extended" and that overwriting needs to end with @override, which is wrong, [latest document][docs on GitHub] has been modified. @section plus @stop, the default is replace (inject), which must be expanded with the @parent keyword. The @override keyword actually has another application scenario.

@show and @stop

What is the difference between the @show @stop the end keyword that corresponds to @section? (Some articles on the web, as well as some editor plugins, will also prompt @endsection, which has been removed in version 4.0, although backwards compatible, but not recommended).

@show refers to the output of the section to the page when it is executed, and @stop is only for content resolution and no longer handles subsequent processing of the section in the current template, unless overridden with @override (see the next section). In general, when defining a section for the first time, you should use a @show, and when you replace it or extend it, you should not use the @show, @stop should be used. The following examples illustrate:

The code is as follows:


{{--Layout.master--}}

@section (' Zonea ')
Aaa
@show








@section (' Zoneb ')
Bbb
@stop








@section (' Zonec ')
Ccc
@show


The code is as follows:


{{--Page.view--}}
@extends (' Layout.master ')

@section (' Zonea ')
Aaa
@stop

@section (' Zoneb ')
Bbb
@stop

@section (' Zonec ')
Ccc
@show

In Layout.master, the "Zoneb" is ended with a @stop, and the block is not displayed because there is no definition of "zoneb" at the end of @show in the entire templating system. In Page.view, the ' Zonec ' is defined with @show, which displays the content as soon as it is executed, and continues to overwrite the content according to the template inheritance mechanism, so the final display will be:

Copy the code code as follows:


CCC//From Page.view

Aaa















Ccc


As you can see from the results, the content of Zoneb is lost because it is not used @show tell the engine to output this part of the content, and Zonec content is displayed two times, and also destroys the Layout.master page structure, because @show appears two times.

@append and @override

Just now, @override don't specify content in the child template to replace the default content of the parent template, but instead, how to use it? This involves an issue in which a section can be used multiple times in a template. That is, each section that we define, in fact, can appear multiple times in the subsequent sub-template. Like what:

The code is as follows:


{{---Master--}}

@yield (' content ')


The code is as follows:


{{--Subview--}}
@extends (' master ')

@section (' content ')
Add a line of content
@append

@section (' content ')
Add another line of content
@append

@section (' content ')
That's enough.
@stop

In the example above, I defined only a section named "Content" in the parent template, and the section was specified three times in the child template. The final output of this example is:

The code is as follows:



Add a line of content
Add another line of content
That's enough.

Three times the specified content is displayed, and the key is to @append the keyword, which indicates that "content here is added to", so the content will continue to expand. In the end, the @stop is used, which means that the section's processing ends. If you continue to use @append or @stop to specify the contents of this section later, it will not take effect. Unless handled with @override. @override means "Overwrite all previous definitions, whichever is the case". Like what:

The code is as follows:


{{---Master--}}

@yield (' content ')
@yield (' message ')



The code is as follows:


{{---Master--}}

@section (' content ')
Add a line of content
@append
@section (' content ')
Add another line of content
@append
@section (' content ')
That's enough, it's over.
@stop
@section (' content ')
No more, I said.
@override


This example is similar to what was just said, except that a set of definitions was added at the end. The final output will be:

The code is as follows:



No more, I said.


So, in a formal project, sometimes you need to traverse the output of the data, you can use the @append, and if you traverse to a data found in front of the wrong? With @override, you can overturn it all.

http://www.bkjia.com/PHPjc/956405.html www.bkjia.com true http://www.bkjia.com/PHPjc/956405.html techarticle laravel template engine blade Some of the tags in section in this article mainly introduced the Laravel template engine Blade section of some of the differences introduced, this article explains the @yield and ...

  • Related Article

    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.