【问题标题】:How to include a template component in Laravel Blade如何在 Laravel Blade 中包含模板组件
【发布时间】:2014-04-07 11:40:42
【问题描述】:

在 Laravel 中进行模板化时,我知道如何拥有一个主模板并在每个视图文件中定义单独的内容区域,但如果我有一个模板代码的 sn-p,我想在我的视图中的某些点包含文件我该怎么做?

为了澄清,假设我有一些带有表单的视图。可能看起来像这样:

@section('content')
    <h3>Login</h3>
{{ Form::open(array('url' => 'login')) }}

        <!-- if there are login errors, show them here -->
      @if ( count($errors->all()) )
      <div class="alert alert-danger">
      <p>Some validation errors occurred:</p>
      <ul>
         @foreach($errors->all() as $error)
         <li>{{ $error }}</li>
         @endforeach
      </ul>
      </div>
      @endif

    <div class="form-group">
        {{ Form::label('email', 'Email Address') }}
    etc...

如您所见,有一个if 语句检查验证错误。这个块是我在我的站点周围的许多表单上可能想要的东西,但是跨模板文件复制粘贴整个块似乎是不好的做法。有没有办法将它存储在一个 views/components.blade.php 文件中,并在需要时生成该特定块?

【问题讨论】:

    标签: templates laravel blade


    【解决方案1】:

    你的问题就是答案

    @if( blabla )
        @include('components')
    @endif
    

    【讨论】:

    猜你喜欢
    • 2015-07-15
    • 2021-01-11
    • 2023-04-07
    • 1970-01-01
    • 2014-03-12
    • 2016-12-18
    • 2013-06-18
    • 2012-10-11
    相关资源
    最近更新 更多