Home  >  Article  >  Backend Development  >  Detailed explanation of the use of Blade templates in Laravel

Detailed explanation of the use of Blade templates in Laravel

黄舟
黄舟Original
2017-09-09 10:06:092014browse

Blade is a simple and powerful template engine provided by laravel. The following article mainly introduces you to the relevant information about the use of Blade templates in the Laravel framework. The article introduces it in great detail through example code, which is very useful for your study or work. It has certain reference and learning value. Friends who need it can take a look below.

Introduction

Blade does not restrict you from using native PHP code in views like other popular PHP template engines. In fact, it Just compile the Blade view into native PHP code and cache it. The cache changes when the Blade view changes, which means Blade adds no compilation burden to your application. Blade view files use the .blade.php suffix and are generally stored in the resources/views directory.

1. Inheritance, fragments, placeholders, components, slots

1.1 Inheritance

1.1.1 Define 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 Fragment

##1.2.1 Parent template definition fragment


@section('part')
// 中间内容即使一个片段
@show

1.2.2 Sub-template filling fragment


@section('part')

Fragment filling content



@endsection

1.3 Placeholder

1.3.1 Parent template placeholder:


@yield('title')

1.3.2 Subtemplate filling placeholder

First filling (text):



@section('title' , '填充的文本占位')

Second filling (text or html)



@section('title')

Filled placeholders


##

@endsection

1.4 Components, slots


1.4.1 Define components

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

<p class=&#39;component&#39;>
 <!-- $title,$content 变量实际上就是预定义的插槽 -->
 <p class=&#39;title&#39;>{{ $title }}</p>
 <p class=&#39;content&#39;>{{ $content }}</p>
</p>

1.4 .2 Using components

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

@component(&#39;component&#39;)
 @slot(&#39;title&#39;)
  组件标题
 @endsolt
 
 @slot(&#39;content&#39;)
  组件内容
 @endslot
@endcomponent

2. Data display

2.1 Escaped output


##
{{ $name }}

2.2 Unescaped output


{!! $name !!}

2.3 Original format output

The first type (not suitable for much):


@{{ name }}

The second type (suitable for large amounts):


@verbatim
{{ name }}
{{ sex }}
{{ age }}
@endverbatim


3. Process control


3.1 for


Note:

There is no $loop variable
  • There is no @empty
  • There is @break
  • # #有@continue

  • ##
    @for ($i = 0; $i < 10; ++$i)
     {{ $i }} <br />
    @endfor
3.2 foreach


Note :

##There is $loop variable

    There is no @empty
  • ## There is @break
  • ##There is @continue

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


Note:


The $loop variable

is required There is @empty

  • There is @break

  • There is @continue

  • @foreach ($data as $k => $v)
     {{ $k }} <br />
    @empty
  • The array has no data
@endforeach


4. Use native PHP



@php 
echo "使用原生 PHP";
@endphp
5. Containing subviews


Note

The included subview can reference 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

  • /**
     * 父视图
     * 父视图拥有变量 $name = &#39;chenxuelong&#39;
     */
    
    <p class=&#39;parent&#39;>
     <p class=&#39;username&#39;>{{ $username }}</p>
     <p class=&#39;child&#39;>
      <!-- 包含子视图 -->
      @include(&#39;child&#39; , [
       &#39;other&#39; => &#39;额外数据&#39;
      ])
     </p>
    </p>
    
    /**
     * 子视图
     */
     <p class=&#39;username&#39;>{{ $username }}</p>
     <p class=&#39;other&#39;>{{ $other }}</p>

  • Summary

The above is the detailed content of Detailed explanation of the use of Blade templates in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn