【问题标题】:Problems with @yield and templating@yield 和模板的问题
【发布时间】:2013-12-23 07:23:52
【问题描述】:

我对 Laravel 很陌生,我正在尝试弄清楚如何使用模板。

我很确定我在这里的东西应该可以工作,但是当我运行它时我一直收到错误消息。我认为这与必须在同一行上使用 @yield 命令有关。这仅仅是刀片引擎的限制吗?

routes.php

Route::get('/', function()
{
    return View::make('hello');
});

// Test Route:
Route::get('jtest', function(){

    $page = array(
        "lang"  => "en",
        "title" => "jtest",
        "css"   => "css/layout.css",
        "rand" => rand()
    );

    return View::make('jtest')->with('page', $page);
});

jtest.blade.php

@extends('layout')

@section('html-lang')
    @if ( isset($page['lang']) )
        {{ $page['lang'] }}
    @endif
@endsection

@section('title')
    @if ( isset($page['title']) )
        {{ $page['title'] }}
    @endif
@endsection

@section('meta-description')
    @if ( isset($page['meta-description']) )
        {{ $page['meta-description'] }}
    @endif
@endsection

@section('css')
    @if ( isset( $page['css'] ) )
        {{ $page['css'] }}
    @endif
@endsection

@section('rand')
    @if ( isset( $page['rand'] ) )
        {{ $page['rand'] }}
    @endif
@endsection

layout.blade.php

<!doctype html>

<html lang="@yield('html-lang')">
<head>
    <meta charset="utf-8">

    <title>@yield('title')</title>
    <meta name="description" content="@yield('meta-description')">
    <meta name="author" content="Jimmy Hogoboom">

    <link rel="stylesheet" href="@yield('css')?r=@yield('rand')">

    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>

<body>



    <script src=""></script>
</body>
</html>

错误是

syntax error, unexpected '='

错误所在的行如下所示:

<link rel="stylesheet" href="<?php echo $__env->yieldContent('css')?r=@yield('rand'); ?>">

所以问题就在这里:

<link rel="stylesheet" href="@yield('css')?r=@yield('rand')">

如果我删除第二个@yield:

<link rel="stylesheet" href="@yield('css')?r=">

页面加载正常。

所以这只是刀片的限制,还是有其他方法我应该将这些值放在页面中?

【问题讨论】:

    标签: laravel laravel-4 blade


    【解决方案1】:

    最好的办法是仅将@yield 和部分用于主要内容或侧边栏等较大的部分。

    如果你需要'isset'条件,你可以使用

    @if(isset($title)){{$title}}@endif
    

    但这也应该在具有干净 php 的控制器上完成,包括变量的默认值。

    【讨论】:

    • 似乎我把事情复杂化了。感谢您的建议。
    【解决方案2】:

    我不知道你为什么需要在一件小事上使用@yield

    下面的简单代码就可以完成这项工作:

     <link rel="stylesheet" href="{{  $page['css'] }}?r={{  $page['rand'] }}">
    

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      相关资源
      最近更新 更多