【问题标题】:Laravel/Blade, template content within contentLaravel/Blade,内容中的模板内容
【发布时间】:2017-01-26 05:04:42
【问题描述】:

我有一个存储“通知”定义的数组。其中两个有我想要模板化的动态变量,但内容本身也是一个模板。

'start_process_import' => array(
    "content" => "Started processing: {{$name}}",
    "view" => '/ajax_templates/notifications/notification',
    'icon' => 'fa-plus',
    'labelStyle' => 'label-success',
    'href' => '/user/view/{{$id}}'
)

如您所见,“content”和“href”索引都包含 Blade 模板化的内容。

然后我将这些数据以及其他数据传递给渲染视图,如下所示:

$response['content'] = view($configView['view'], $data)->render());

这里是视图模板文件,它确实得到了刀片模板。

       <li>
<a href="{{ $href }}">
    <span class="time">{{ $date }}</span>
    <span class="details">                                                       
        <span class="label label-sm label-icon {{ $labelStyle }}">
            <i class="fa {{$icon}}"></i>
        </span>
        {{$content}} 
    </span>
</a>

这是输出:

       <li>
<a href="/user/view/{{$id}}">
    <span class="time">2016-09-18 11:37:37</span>
    <span class="details">                                                       
        <span class="label label-sm label-icon label-success">
            <i class="fa fa-plus"></i>
        </span>
        Started processing: {{$name}} 
    </span>
</a>

$id 和 $name 变量仍然存在,并且没有被 Blade 模板格式化。

有没有办法将视图“加倍”刀片模板?

【问题讨论】:

    标签: php laravel templates laravel-5.2 blade


    【解决方案1】:

    如果确实需要重新渲染模板两次,则需要使用String Blade Compiler包或类似的包。

    【讨论】:

      【解决方案2】:

      您可以访问模板中的变量:$name$id,对吧?下面将提供一个建议,这可能是有用的。但是,没有办法知道它是否有效。建议如下:

      <?php
      
          'start_process_import' => array(
              "content" => "Started processing: ",     //<== REMOVED DYNAMIC VARIABLE: $name
              "view" => '/ajax_templates/notifications/notification',
              'icon' => 'fa-plus',
              'labelStyle' => 'label-success',
              'href' => '/user/view/'                 //<==  REMOVED DYNAMIC VARIABLE: $id
          );
      

      刀片模板

         <li>
             <!-- SINCE BOTH $id & $name ARE AVAILABLE TO YOU IN THE CURRENT TEMPLATE -->
             <!-- YOU COULD STRIP THE DYNAMIC VARIABLES($name & $id) FROM THE content & href KEYS -->
             <!-- AND PERHAPS ADD THEM DIRECTLY HERE WITHIN THE TEMPLATE -->
              <a href="{{ $href }}{{ $id }}">
                  <span class="time">{{ $date }}</span>
                  <span class="details">                                                       
                      <span class="label label-sm label-icon {{ $labelStyle }}">
                          <i class="fa {{$icon}}"></i>
                      </span>
                      {{ $content }}{{ $name }}
                  </span>
              </a>
         </li>
      

      【讨论】:

      • 有些内容不会为其他模板设置“$id”,有些会。
      • 'subscription_complete' => array( "content" => "已注册 {{$package->name}} 包!", "view" => '/ajax_templates/notifications/notification' , 'icon' => 'fa-ticket', 'labelStyle' => 'label-success', 'href' => '/user/billing/info')
      猜你喜欢
      • 2015-07-15
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2019-04-03
      • 2018-09-23
      • 2013-06-21
      • 1970-01-01
      相关资源
      最近更新 更多