The usage of blade template in Laravel

Source: Internet
Author: User
Tags php template
Blade is a simple and powerful template engine provided by Laravel, the following article mainly introduces the Laravel framework of Blade template usage Related Materials, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, The friends who need to take a look below. We hope to help you.

Brief introduction

Blade It doesn't limit you to using native PHP code in your view like any other popular PHP template engine, in fact it compiles Blade views into native PHP code and caches them. The cache changes when the Blade view changes, which means that Blade does not add the burden of compiling your app. The Blade view file uses the. blade.php suffix, which is typically stored in the Resources/views directory.

1. Inheritance, fragments, placeholders, components, slots

1.1 Inheritance

1.1.1 Defining a parent template

laravel/resources/views/base.blade.php

1.1.2 Child Template Inheritance

Path: laravel/resources/views/child.blade.php

@extends (' base ')

1.2 Fragments

1.2.1 Parent Template Definition Fragment

@section (' part ')//intermediate content even if a fragment @show

1.2.2 Child template Fill Fragment

@section (' part ')

Fragment Fill Content

@endsection

1.3 Placeholder

1.3.1 Parent Template placeholder:

@yield (' title ')

1.3.2 Sub-template fill placeholder

First Fill (text):

@section (' title ', ' filled-in text placeholder ')

Second fill (text or HTML)

@section (' title ')

Placeholder for fill

@endsection

1.4 Components, Slots

1.4.1 Defining components

Path: laravel/resources/views/component.blade.php

<p class= ' component ' > <!--$title, $content variables are actually predefined slots--<p class= ' title ' >{{$title}}</p> & Lt;p class= ' content ' >{{$content}}</p></p>

1.4.2 Using Components

Path: laravel/resources/views/test.blade.php

@component (' component ') @slot (' title ')  component title @endsolt  @slot (' content ')  component content @endslot @endcomponent

2. Data display

2.1 Escape output

{{$name}}

2.2 Output not escaped

{!! $name!}

2.3 Original Format output

The first one (not much for the amount):

@{{name}}

The second kind (suitable quantity is many):

@verbatim {{Name}}{{sex}}{{Age}} @endverbatim

3. Process Control

3.1 for

Attention:

    • No $loop variables

    • No @empty

    • Have @break

    • Have @continue

@for ($i = 0; $i < + + $i) {{$i}} <br/> @endfor

3.2 foreach

Attention:

    • There are $loop variables

    • No @empty

    • Have @break

    • Have @continue

@foreach ($data as $k = = $v) {{$k}} <br/> @endforeach

3.3 Forelse

Attention:

    • There are $loop variables

    • Must have @empty

    • Have @break

    • Have @continue

@foreach ($data as $k = = $v) {{$k}} <br/> @empty

Array has no data

@endforeach

4. Using native PHP

@php echo "Using native PHP"; @endphp

5. Include child views

Attention

    • The contained child views can refer to all variables defined by the parent view.

    • You can pass additional data to the child view

Define the parent view parent.blade.php, and include the child View child.blade.php, and pass in additional data

/** * Parent View * Parent view has variable $name = ' chenxuelong ' */<p class= ' parent ' > <p class= ' username ' >{{$username}}</p> & Lt;p class= ' child ' >  <!--contains sub-views  -@include (' Children ', [' other ' + '   extra data '  ]) </p> </p>/** * Sub View */<p class= ' username ' >{{$username}}</p> <p class= ' other ' >{{$other}}</p>

Related recommendations:

Explore how the middleware of Laravel is implemented

Laravel Optimized Split routing file

Laravel writing the App interface (API)

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.