laravel 如何在 Blade 模板中包含子视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21753954/
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
How to include a sub-view in Blade templates?
提问by Benubird
I am trying to set up a site using laravel, but I'm really having trouble with basic things that the documentation just doesn't cover.
我正在尝试使用 laravel 建立一个站点,但我真的在文档没有涵盖的基本内容方面遇到了麻烦。
In this case, I see that it says I can include one view inside another by using @include('view.name')
. What is view.name? Where is it saved? I tried creating a file app/views/view.name.blade.php
, but it wasn't read. How does the file name map to the blade name?
在这种情况下,我看到它说我可以通过使用将一个视图包含在另一个视图中@include('view.name')
。什么是view.name?它保存在哪里?我尝试创建一个文件app/views/view.name.blade.php
,但没有读取。文件名如何映射到刀片名称?
采纳答案by winkbrace
EDIT: Below was the preferred solution in 2014. Nowadays you should use @include
, as mentioned in the other answer.
编辑:以下是 2014 年的首选解决方案。如今您应该使用@include
,如另一个答案中所述。
In Laravel views the dot is used as folder separator. So for example I have this code
在 Laravel 视图中,点用作文件夹分隔符。所以例如我有这个代码
return View::make('auth.details', array('id' => $id));
which points to app/views/auth/details.blade.php
指向 app/views/auth/details.blade.php
And to include a view inside a view you do like this:
并在视图中包含一个视图,您可以这样做:
file: layout.blade.php
文件:layout.blade.php
<html>
<html stuff>
@yield('content')
</html>
file: hello.blade.php
文件:hello.blade.php
@extends('layout')
@section('content')
<html stuff>
@stop
回答by Gaz_Edge
You can use the blade template engine:
您可以使用刀片模板引擎:
@include('view.name')
'view.name' would live in your main views folder:
'view.name' 将位于您的主视图文件夹中:
// for laravel 4.X
app/views/view/name.blade.php
// for laravel 5.X
resources/views/view/name.blade.php
Another example
另一个例子
@include('hello.world');
would display the following view
将显示以下视图
// for laravel 4.X
app/views/hello/world.blade.php
// for laravel 5.X
resources/views/hello/world.blade.php
Another example
另一个例子
@include('some.directory.structure.foo');
would display the following view
将显示以下视图
// for Laravel 4.X
app/views/some/directory/structure/foo.blade.php
// for Laravel 5.X
resources/views/some/directory/structure/foo.blade.php
So basically the dot notation defines the directory hierarchy that your view is in, followed by the view name, relative to app/views
folder for laravel 4.x or your resources/views
folder in laravel 5.x
所以基本上点符号定义了你的视图所在的目录层次结构,然后是视图名称,相app/views
对于 laravel 4.x 的resources/views
文件夹或你在 laravel 5.x 中的文件夹
ADDITIONAL
额外的
If you want to pass parameters: @include('view.name', array('paramName' => 'value'))
如果要传递参数: @include('view.name', array('paramName' => 'value'))
You can then use the value in your views like so <p>{{$paramName}}</p>
然后,您可以像这样在视图中使用该值 <p>{{$paramName}}</p>
回答by Kent Allen Sison
As of Laravel 5.6, if you have this kind of structure and you want to include another blade file inside a subfolder,
从 Laravel 5.6 开始,如果您有这种结构并且想要在子文件夹中包含另一个刀片文件,
|--- views
|--- 意见
|------- parentFolder (Folder)
|------- parentFolder(文件夹)
|---------- name.blade.php (Blade File)
|---------- name.blade.php (刀片文件)
|---------- childFolder (Folder)
|----------- childFolder(文件夹)
|-------------- mypage.blade.php (Blade File)
|-------------- mypage.blade.php(刀片文件)
name.blade.php
名称.blade.php
<html>
@include('parentFolder.childFolder.mypage')
</html>