2014-09-02 97 views
4

我是Laravel的新手,我正在尝试为我制作的新网站创建我的第一个布局。Laravel头部内​​容出现在身体标记

我遇到的问题是我想要的内容<head>正在进入<body>,<head>为空。

我:

layout.blade.php

<!DOCTYPE html> 
<html> 
    <head> 
     <title>@section('title')</title> 
     {{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'); }} 
    </head> 

    <body> 
     <h1>@yield('h1')</h1> 
     @yield('content') 
     {{ HTML::script('js/jquery-1.11.1.min.js'); }} 
     {{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }} 
    </body> 
</html> 

users.blade.php

@extends('layouts.layout') 

@section('title') 
    Users Page - 1 

@section('h1') 
    <?PHP echo $h1;?> 
@stop 

我要去哪里错了?

又是什么区别@yeild@section在我看来?

+0

可能有其他地方在页面上成为一个问题,您的浏览器刚刚重组。如果您查看源代码,头标记中的项目可能实际上位于头标记中。您也需要在段标签后加上@ @ stop。这可能是导致问题 – user3158900 2014-09-02 18:40:39

+0

哇,帮我以及 – ronydavid 2017-11-03 06:12:19

回答

0

试试这个layout.blade.php。您正在扩大这一刀模板,所以你应该<title>标签使用@yield('title'),你应该@stop@section

<!DOCTYPE html> 
<html> 
    <head> 
     <title>@yield('title')</title> 
     {{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'); }} 
    </head> 

<body> 
    <h1>@yield('h1')</h1> 
    @yield('content') 
    {{ HTML::script('js/jquery-1.11.1.min.js'); }} 
    {{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }} 
</body> 

+0

谢谢,我错过了@stop – imperium2335 2014-09-03 08:50:05

2

其实你应该使用yield倾倒出的内容,例如,如果你有一个mastwer布局,如:

extends('layouts.master') 

@section('content') 
    Everything within this section will be dumped 
    in to `@yield('content')` in your master layout. 
@stop 

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
    <body> 
     @yield('content') 
    </body> 
</html> 

然后你可以在你的孩子鉴于这样的使用

对于任何部分,您应该使用@stop来关闭该部分。也许你可以尝试这样的事:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>{{ $title }}</title> 
     {{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css') }} 
    </head> 
    <body> 

     @yield('content') 

     {{ HTML::script('js/jquery-1.11.1.min.js'); }} 
     {{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }} 
    </body> 
</html> 

从你controller您可以通过标题,如:

return View::make('viewname')->with('title', $title); 

了解更多关于在上TemplatesLaravel website

+0

+1的详细信息。你认为最好的做法是通过视图或控制器发送诸如标题数据之类的东西(如返回View :: make('viewname') - > with('title',$ title);而不是使用' @yeild''@section'etc? – imperium2335 2014-09-03 08:54:32

+0

是的,它更具动态性 – 2014-09-03 16:40:42

+0

对不起,您是指通过模板或视图? – imperium2335 2014-09-03 16:56:54

相关问题