使用 laravel Blade 嵌套视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16950756/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:53:43  来源:igfitidea点击:

Nesting views with laravel Blade

laravellaravel-4blade

提问by Michael

I have searched SO, and dug in the Laravel documentation but I am not sure I quite understand if what I would like to do can be done.

我已经搜索了 SO,并在 Laravel 文档中进行了挖掘,但我不确定我是否完全理解我想做的事情是否可以完成。

I am using Laravel 4. I want to know how I can nest views in other views.

我正在使用 Laravel 4。我想知道如何在其他视图中嵌套视图。

For example, I have a base layout.. lets call it layout.blade.php

例如,我有一个基本布局..让我们称之为 layout.blade.php

<html>
    <head>
      <title>{{ $title }}</title>
    </head>
    <body>
        @yield('nav')  
        @yield('content')
    </body>
</html>

Next I have a blade for a page called home:

接下来,我有一个名为的页面的刀片home

@extends('layout')

@section('nav')
<p>NAVIGATION</P>
@end

@section('content')
<p>HELLO WORLD!</P>
@end

I have a couple different navigation layouts, one for admins, another for super users, and another for regular users.

我有几种不同的导航布局,一种用于管理员,另一种用于超级用户,另一种用于普通用户。

Is there a way to add another blade view inside the section('nav')?

有没有办法在里面添加另一个刀片视图section('nav')

@section('nav')
// do something magical here?
@end

It doesn't make sense that for every blade layout I need to repeat the navigation code when several snippets can be reused.

当可以重复使用多个片段时,对于每个刀片布局我都需要重复导航代码是没有意义的。

回答by Antonio Carlos Ribeiro

You can do this

你可以这样做

@section('nav')
  @include('another')
  @include('magical')
  @include('snippet')
@end

回答by Bede

Another solution, in case you were wishing to dynamically load different subviews, you can nest using the ViewClass. E.g. you could have the following in a Route / Controller:

另一种解决方案,如果您希望动态加载不同的子视图,您可以使用 Class.js 嵌套View。例如,您可以在路由/控制器中包含以下内容:

return View::make('home')->nest('subnav','home/nav', array('some' => 'data'); 

and then in your home.blade.php, you could do this:

然后在你的home.blade.php,你可以这样做:

@extends('layout')

@section('nav')
<p>NAVIGATION</p>
{{ $subnav }}
@end

@section('content')
<p>HELLO WORLD!</p>
@end

This can be done with an include and a variable as well (@include($viewname, array('some' => 'data')) however I'd say its cleaner as it removes the logic from the view, particularly if your nested views aren't always the same blade file.

这也可以通过包含和变量 ( @include($viewname, array('some' => 'data'))来完成,但是我会说它更清晰,因为它从视图中删除了逻辑,特别是如果您的嵌套视图并不总是相同的刀片文件。

回答by Blank EDjok

Even though this is late you can also do this:

即使已经晚了,您也可以这样做:

eg. in an admin.php you can have this:

例如。在 admin.php 你可以有这个:

@extends('home')

@section('nav')
  // navigation
@endsection

@section('content')
  // admin page content
@endsection

Not saying this is better or not i'm just answering your question on nesting views with blade, this is how i nest my views.

不是说这更好与否,我只是回答您关于使用刀片嵌套视图的问题,这就是我嵌套视图的方式。