【问题标题】:Laravel 4 Blade weird behaviourLaravel 4 Blade 奇怪的行为
【发布时间】:2013-11-06 03:28:06
【问题描述】:

所以,我有这样的代码来打印我的索引页:

class PageController extends MediaController {

protected $layout = 'layouts.main';

public function index_page() {
    $data = array();

    $data['title'] = 'Dynamic Title';
    $data['css_files'] = array(
        array('media'=>'all', 'file'=>'file1'),
        array('media'=>'all', 'file'=>'file2')
    );

    $this->layout->content = View::make('index', $data);
}

}

还有我的 main.blade.php:

<html>

<head>
    <title>@yield('title')</title>
</head>

<body>
    @yield('content')
    @yield('css_files')
</body>

</html>

还有我的 index.blade.php:

@section('title', $title)

@section('css_files')
    @foreach ($css_files as $css_file)
        <p>File: {{ $css_file->file }}, Media: {{ $css_file->media }}</p>
    @endforeach
@stop

@section('content')
    <h1>Rendered Successfully!</h1>
@stop

标题渲染得很好,但是 css 文件打印了这个:

文件:{{ $css_file->file }},媒体:{{ $css_file->media }}

文件:{{ $css_file->file }},媒体:{{ $css_file->media }}

而不是这个:

文件:file1,媒体:全部

文件:file2,媒体:全部

谁能解释为什么?感谢您的帮助,我对 Blade 很陌生。

--编辑--

我已经解决了这个问题,我碰巧在

中编辑了Blade语法配置

供应商\laravel\framework\Illuminate\view\compilers\BladeCompiler.php

来自

protected $contentTags = array('{{', '}}');

    /**
     * Array of opening and closing tags for escaped echos.
     *
     * @var array
     */
    protected $escapedTags = array('{{{', '}}}');

protected $contentTags = array('{=', '=}');

    /**
     * Array of opening and closing tags for escaped echos.
     *
     * @var array
     */
    protected $escapedTags = array('{={', '}=}');

所以我应该使用 {= 而不是 {{

希望这对将来的人有所帮助。

【问题讨论】:

  • 为什么你必须使用{=?通常不应修改 vendor 文件夹,因为如果您通过 composer 更新它,您的更改将被覆盖。
  • 用来结合blade和angular.js,不得不改语法
  • 现在很有意义

标签: php laravel laravel-4 blade


【解决方案1】:

您在标题部分下方缺少@stop。试着把它变成这样

@section('title')
  {{ $title }}
@stop

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 2018-05-27
    • 2023-03-13
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2020-11-26
    相关资源
    最近更新 更多