Вопрос

I'm very new to Laravel and I'm trying to figure out how to use the templating.

I feel so sure that what I have here should work, but I keep getting an error when I run it. I think it has to do with having to @yield commands on the same line. Is this simply a limitation of the blade engine?

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>

And the error is

syntax error, unexpected '='

and the line the error's on looks like this:

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

So the problem is here:

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

If I remove the second @yield:

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

the page loads fine.

So is this just a limitation of blade, or is there some other way I should be placing these values in the page?

Это было полезно?

Решение

The best idea is to use @yield and sections only for bigger parts like main content or for a sidebar.

If you need 'isset' condition you can use

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

But this also should be done on controller with clean php including default value for a variable.

Другие советы

I am not sure why you need to use @yield for a small thing.

the following simple code will do the job:

 <link rel="stylesheet" href="{{  $page['css'] }}?r={{  $page['rand'] }}">
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top