Laravel 部分没有出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14070936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Section not appear
提问by Patrick Maciel
I'll follow some tutorials about Laravel 3, and know, I have a problem, with one:
我将遵循一些有关 Laravel 3 的教程,并且知道,我遇到了一个问题:
@section('post_navigation')
@if(Auth::check())
@include('plugins.loggedin_postnav')
@endif
@endsection
Why this section not appear?
为什么这个部分没有出现?
I try remove all content in section, for example;
例如,我尝试删除部分中的所有内容;
@section('post_navigation')
<h1>Test</h1>
@endsection
But doesn't work.
但不起作用。
The complete code is that:
完整的代码是:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
{{ HTML::link('/', 'Instapics', array('class' => 'brand')); }}
<div class="nav-collapse">
<ul class="nav">
@section('navigation')
<li class="active">{{ HTML::link('/', 'Home') }}</li>
@yield_section
</ul>
</div>
@section('post_navigation')
@if(Auth::check())
@include('plugins.loggedin_postnav')
@endif
@endsection
</div>
</div>
</div>
[EDIT]
I change @endsection to @yield_section, and works, BUT, I still not understand, for example (in User_Controller index view):
[编辑]
我将@endsection 更改为@yield_section,并且有效,但是,我仍然不明白,例如(在 User_Controller 索引视图中):
@section('post_navigation')
@parent
@endsection
Why not appear the include?
为什么不出现包含?
Why I need change endsectionto yield_section?
为什么我需要将endsection更改为yield_section?
回答by Bilal Gultekin
When you use @section, you define a section like defining a variable. Then, you can use it where you want by @yield('sectionname')
(or by using another way I specified in the second paragraph). For example, you can look at this:
当您使用@section 时,您定义了一个部分,就像定义一个变量一样。然后,您可以在需要的地方使用它@yield('sectionname')
(或使用我在第二段中指定的另一种方式)。例如,你可以看看这个:
@section('scripts')
<script src="jquery.js"></script>
@endsection
<head>
@yield('scripts')
</head>
@section / @endsection and @section / @yield_section are not same. @section / @yield_section defines a section area, not a section. In other words, it calls a variable. Actually it is more similar to yield('sectionname') than @section / @endsection. It has default value as a main difference from yield.
@section / @endsection 和 @section / @yield_section 不一样。@section / @yield_section 定义了一个部分区域,而不是一个部分。换句话说,它调用一个变量。实际上它比@section / @endsection 更类似于yield('sectionname')。它具有默认值作为与产量的主要区别。
You can define an area which has default value, and then you can change it by defining a section. This logic mostly used while creating and using layouts. For example, Let below page be our main (main.blade.php) layout.
您可以定义一个具有默认值的区域,然后您可以通过定义一个部分来更改它。此逻辑主要在创建和使用布局时使用。例如,让下面的页面成为我们的主要(main.blade.php)布局。
<html>
<head>
<title>
@section('title')
Default Title Maybe Sitename.com
@yield_section
</title>
@include('scripts')
</head>
<body>
<div id="content">
@section('content')
Default content
@yield_section
</div>
<div id="sidebar">
@section('sidebar')
<ul id="menu">
<li><a href="#">Menu item 1</a></li>
<li><a href="#">Menu item 2</a></li>
<li><a href="#">Menu item 3</a></li>
<li><a href="#">Menu item 4</a></li>
</ul>
@yield_section
</body>
</html>
Generally layouts are not used directly. Another page is created and specified in the page its layout like @layout('main')
. Then, sections are defined and laravel templating system change the defined section. Let this page be our post (post.blade.php) page:
一般不直接使用布局。创建另一个页面并在页面中指定其布局,如@layout('main')
. 然后,定义部分并且 Laravel 模板系统更改定义的部分。让这个页面成为我们的帖子(post.blade.php)页面:
@layout('main')
@section('title')
$post->title - @parent
@end_section
@section('content')
$post->content
@end_section
When we return post view View::make('profile');
, as you can see, layout logic will work and sections in main.blade.php will be changed with we defined in this page. So, the output will be:
当我们返回 post view 时View::make('profile');
,如您所见,布局逻辑将起作用,并且 main.blade.php 中的部分将根据我们在此页面中的定义进行更改。所以,输出将是:
<html>
<head>
<title>
Post title - Default Title Maybe Sitename.com
</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div id="content">
Post content
</div>
<div id="sidebar">
<ul id="menu">
<li><a href="#">Menu item 1</a></li>
<li><a href="#">Menu item 2</a></li>
<li><a href="#">Menu item 3</a></li>
<li><a href="#">Menu item 4</a></li>
</ul>
</body>
</html>
By the way, @parent
returns default value in section area and @include
is same logic with include in php.
顺便说一句,@parent
在节区域中返回默认值,@include
与php中的include逻辑相同。
Have a nice day.
祝你今天过得愉快。
回答by csm232s
Seems like you may have found an answer. But it looks like @endsection has been replaced with @stop in L4. Try something like this instead:
似乎您已经找到了答案。但看起来@endsection 已被 L4 中的 @stop 替换。试试这样的:
@section('scripts')
<script src="jquery.js"></script>
@stop