Laravel 5.3 Blade Template new $loop variable learning notes

Source: Internet
Author: User
Tags php code

What is a blade command?

The blade template language of Laravel provides instruction functionality, which is actually a custom label--at the beginning of the @, usually used to control the structure. If you write a template with a blade, you will be familiar with the commands like @if, @foreach.

These directives are usually equivalent to the corresponding PHP code, such as @if (condition) equivalent to <?php if ($condition):

$loop variables

In Laravel 5.3, @foreach directives provide more powerful functionality, and a new $loop variable can be invoked in each @foreach loop body. The variable is a Stdclass instance that contains the metadata information for the current loop, let's take a look at the properties it provides:

Index: Circular index starting at 1, 1 means the first entry.
Remaining: The number of entries in the loop, such as the one currently in the 3, returns 2;
Count: Number of loops total entries
First: Whether it is the number one
Last: Whether it is the final
Depth: The level of the cycle
Parent: If the loop is in another @foreach, return the parent circular reference, or null

The following is a sample code:

<ul>
@foreach ($pages as $page)
<li>{{$page->title}} ({{$loop->index}}}/{{$loop->count}}) </li>
@endforeach
</ul>

If there is a nested loop, you can use depth to determine and obtain the appropriate information through the $loop Parent property:

<ul>
    @foreach ($pages as $page)
        <li >{{$loop->index}}: {{$page->title}}
        @if ($page-> HasChildren ())
        <ul>
             @foreach ($page->children () as $child)
                 <li>{{$loop->parent->index}}. {{$loop->index}}:
                 {{$child->title}}</li>
            @ Endforeach
        </ul>
         @endif
    </li>
    @endforeach
</ul>

The

is so simple and convenient.

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.