laravel模板机制

Posted 微先锋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel模板机制相关的知识,希望对你有一定的参考价值。

@extends(‘layouts.master‘)

@section(‘title‘, ‘Page Title‘)

@section(‘sidebar‘)
    @parent
    <p>This is appended to the master sidebar.</p>
@endsection

@section(‘content‘)
    <p>This is my body content.</p>
@endsection
{{ isset($name) ? $name : ‘Default‘ }}  = {{ $name or ‘Default‘ }}  等价
@{{ name }}  直接输出

@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don‘t have any records!
@endif

和IF相反
@unless (Auth::check())
    You are not signed in.
@endunless


@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

@while (true)
    <p>I‘m looping forever.</p>
@endwhile

@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>This is user {{ $user->id }}</p>
@endforeach



@foreach ($users as $user)
    @foreach ($user->posts as $post)
        @if ($loop->parent->first)
            This is first iteration of the parent loop.
        @endif
    @endforeach
@endforeach


 @include(‘shared.errors‘)


堆栈

Blade允许你推送内容到命名堆栈,以便在其他视图或布局中渲染。这在子视图中引入指定javascript库时很有用:

@push(‘scripts‘)
    <script src="/example.js"></script>
@endpush

推送次数不限,要渲染完整的堆栈内容,传递堆栈名称到 @stack 指令即可:

<head>
    <!-- Head Contents -->

    @stack(‘scripts‘)
</head>


{{time()}}

























以上是关于laravel模板机制的主要内容,如果未能解决你的问题,请参考以下文章

@include Laravel Blade 模板以使用 Javascript 追加

laravel特殊功能代码片段集合

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段1——vue主模板

VSCode自定义代码片段2——.vue文件的模板