Introduction to some labels of section in Laravel template engine Blade _ PHP Tutorial

Source: Internet
Author: User
The difference between some labels of section in Laravel template engine Blade. The difference between some labels in the section in the Laravel template engine Blade this article mainly introduces the differences between some labels in the section in the Laravel template engine Blade, this article explains the differences between some labels of section in @ yield and Laravel template engine Blade.

This article mainly introduces the differences between some labels of section in Laravel template engine Blade, this article explains the differences between @ yield and @ section, @ show and @ stop, @ append, and @ override. For more information, see

The Blade template engine in the Laravel framework is very useful, but the Blade introduction in the official documentation is not detailed. some of the content has not been written, while others are not clear. For example, you may encounter the following problems:

1. what is the difference between @ yield and @ section, which can both be predefined and can be replaced?
2. @ section can end with @ show, @ stop, @ overwrite, and @ append. What are the differences between the three?

This article gives a simple but intuitive introduction to these problems.

@ Yield and @ section

First, @ yield is not extensible. if the part you want to define has no default content to extend the sub-template, it is more convenient to use @ yield ($ name, $ default, if you do not specify the content of the block in the subtemplate, the default content is displayed. if you define the content, the content you define is displayed. Either.

In contrast, @ section can be replaced and expanded, which is the biggest difference. For example:

The code is as follows:


{-- Layout. master --}}
@ Yield ('title', 'Default title ')

@ Section ('content ')
Default content
@ Show

The code is as follows:


{-- Home. index --}}
@ Extends ('layout. master ')

@ Section ('title ')
@ Parent
New Title
@ Stop

@ Section ('content ')
@ Parent
Extended content
@ Stop

In the preceding example, the template defines a block using @ yield and @ section respectively, and then defines the content in the subtemplate. because @ yield cannot be extended, therefore, even if @ parent is added, the output content is only "New title", replacing "default title ". Therefore, the generated page can only be the "default title" or "New title", and cannot coexist. The @ section definition uses the @ parent keyword, and the content in the parent template will be retained and added after extension, the output content is "default content Extended content ".

The document on the official website does not contain the @ parent keyword. it means that the default behavior is "extended" and @ override is used to overwrite the content. this is wrong, [Latest github documentation] [docs] has been fixed. @ Section and @ stop are added. by default, replace (injection) is used. the @ parent keyword must be used for extension. The @ override keyword has another application scenario.

@ Show and @ stop

Next, let's talk about the ending keyword corresponding to @ section. what is the difference between @ show and @ stop? (Some articles on the internet and some editor plug-ins will prompt @ endsection, which has been removed in version 4.0 and is backward compatible but not recommended ).

@ Show refers to outputting the content in this section to the page when it is executed here, while @ stop only parses the content and does not process the subsequent processing of this section in the current template, unless it is overwritten by @ override (see the next section ). Generally, @ show should be used when defining a section for the first time. instead of @ show, @ stop should be used when replacing or extending it. The following is an example:

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, @ stop is used to end "zoneB". because there is no "zoneB" definition ending with @ show in the template system, this block will not be displayed. In the page. in view, @ show is used to define 'zonec '. This will immediately display the content and continue to overwrite the content according to the template inheritance mechanism. Therefore, the final content displayed will be:

The code is as follows:


Ccc // from page. view


Aaa















Ccc


As you can see from the results, zoneB's content is lost because @ show is not used to tell the engine to output this part of content, while zoneC's content is displayed twice, and layout is damaged. the page structure of the master, because @ show appears twice.

@ Append and @ override

As mentioned just now, @ override does not specify in the sub-template that the content replaces the default content of the parent template, but is used for another purpose. how can this problem be used? This involves the issue that a section can be used multiple times in the template. That is, each section we define can appear multiple times in subsequent subtemplates. For example:

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 a line of content
@ Append

@ Section ('content ')
Enough. so far.
@ Stop

In the preceding example, only one section named "content" is defined in the parent template, and the content of this section is specified three times in the subtemplate. The final output of this example is:

The code is as follows:



Add a line of content
Add a line of content
Enough. so far.

The specified content is displayed three times. The key lies in the @ append keyword, which indicates that "the content here is added to", so the content will be expanded continuously. At last, @ stop is used to indicate the processing of this section. If you continue to use @ append or @ stop to specify the content of this section, it will not take effect. Unless @ override is used for processing. @ Override: "overwrite all previous definitions. this time prevails ". For example:

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 a line of content
@ Append
@ Section ('content ')
Enough. end it.
@ Stop
@ Section ('content ')
No, I said.
@ Override


This example is similar to the previous one, but a set of definitions are added at last. The final output will be:

The code is as follows:



No, I said.


Therefore, in a formal project, you can use @ append to traverse and output data. if you traverse a data, you will find that all the preceding data is incorrect? You can use @ override to overturn all of them.

This article mainly introduces the differences between some labels of section in Laravel template engine Blade. This article explains @ yield and...

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.