- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有人知道是否可以延长 child Blade 吗?
我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列 @includes。
@extends('common.layout')
@section('content')
... Some other content ...
@include('modals.modal-1')
@include('modals.modal-2')
@endsection
所有的模态都有很多通用的样板代码(Bootstrap),所以我想做的是定义一个主模型模板,让所有的模态从它@extend,然后在我的页面中@include那些作为必需的。所以/modals/modal-1.blade.php 看起来像这样:
@extends('common.modals')
@section('modal-id', 'modal-1')
@section('modal-title','Modal title')
@section('modal-body')
... Some HTML / Blade here ...
@endsection
但每次尝试时,生成的 HTML 都会损坏。在上述情况下,modal-1 会显示两次,因为它首先出现,而 modal-2 根本不会出现。颠倒顺序,modal-2 将出现两次。
我想我可以简单地将模态主体放在一个变量中并在@include 语句中使用它 @include('modal.blade', ['body' => '<div>...</div>'])
, 但使用 @extends 感觉更正确。
有什么想法吗?
最佳答案
您完全可以从已经扩展的 View 扩展 Blade View 。但是,您将模板继承与 View 包含混合在一起,这会导致您出现奇怪的结果。
当使用 @extend
指令继承模板时,您必须始终返回您想要的链上最低的子级。假设您有 3 代模板:
//grandparent.blade.php
<html>
<head>
<title>My App</title>
</head>
<body>
@yield('parent-content')
</body>
</html>
//parent.blade.php
@extends('grandparent')
@section('parent-content')
<div class="container">
@yield('child-content')
</div>
@endsection
//child.blade.php
@extends('parent')
@section('child-content')
<div class="page">
//stuff
</div>
@endsection
在这种情况下,您将返回 subview ,它还将包含其上方的两代模板。但是您可以永远返回parent.blade.php
并期望它也返回那个 subview 。可能有 100 个扩展父 View 的 subview ,因此无法知道是哪一个。
将 @include
指令视为一种将 View 中的 HTML 很好地分解成更小部分的方法。通常,您会将它用于要在多个 View 中引用的可重用代码片段。不过,它与模板继承不同。还要记住,包含的 View 将获得与其父 View 相同的所有数据(您甚至可以传递更多数据)。
在您的情况下,您必须决定什么构成页面的基本根。页面的核心是modal-1
吗?如果是这样,您需要从您的 Controller 返回 modal-1
作为您的 subview 并将其向上扩展。在这种情况下,请将文件完全保留在您的帖子中。它的父 View (common.modals
) 需要更改为:
@extends('common.layout')
@section('content')
... Some other content ...
@yield('modal-id')
@yield('modals-title')
@yield('modal-body')
@include('modals.modal-2')
@endsection
显然,您会将每个 yield 语句放在页面上有意义的地方。
但是,如果 modal-1
不是页面的核心,而只是您想要包含的额外内容(如小部件),那么您应该 include
它就像您在父 View 中所做的那样。在这种情况下,您将需要从中删除 @extends
指令,并且不必费心将主要 HTML 包装在任何部分中。它将按原样传递给 View 。如果您在包含的模板中包含部分,则必须在包含它的 View 中生成这些部分。所以你的 modal-1
模板最终会看起来像这样:
<div>
<p>HTML goes here. No need to extend anything or wrap in a section.
You can still include {{$data}} though.
</p>
</div>
@section('script')
Including this section means that the view that is including this template
already contains a @yield('script') directive.
By including the @parent directive, this section will append that one.
@parent
@endsection
关于php - Laravel Blade : @extend a (already extended) child blade?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344580/
这段代码: interface I {} public class Test { TableView table; Test(ObservableList list) {
我们从 .NET 2.0 升级到 .NET 3.5。我的一位同事尝试在 Visual Studio 2008 中的调试器下运行 ASP .NET Web 项目时出现以下对话框。他可以正常构建,但无法调
我有一个具有class User extends Authenticatable的用户模型类,并且我也创建了另一个具有class Foo extends Model的模型类 这会在显示路线文件中的数据
我遇到的一个常见问题 @extend当试图用另一个 @extend 覆盖继承的属性时. 这是一个例子: // class selectors to be @extended // these coul
我对以下代码的 typescript 编译错误感到困惑: function f(x: T, y: S) { if (x === y) { // ERROR: This condition
这与对象 {} === {} 无关, found this issues不知道这个是不是一样 类型集 - AUnion 不是空集。另外两种类型(L 和R)正在扩展它。我的理解是这些 L、R 至少和 A
我收到以下错误: Extender Provider failed to return an Extender for this object 尝试为 .Net v4.7.2 加载 WCF 项目时。我
我收到以下错误: Extender Provider failed to return an Extender for this object 尝试为 .Net v4.7.2 加载 WCF 项目时。我
我刚刚在读Javascript: Module Pattern vs Constructor/Prototype pattern?我很好奇,当我们使用 $.fn.extend 或 $.extend 扩
我正在用 extend 做一些测试,在我做了一些观察后我有点困惑。初步观察: console.log($.extend === $.fn.extend); // trure // and since
我一直在使用一些通用方法从元素的可变参数创建集合,例如 public Set createSet( T... elements ) { ... 然而,最近我遇到了编译器没有按照我的预期去做的情况。以
刚去面试,问了一个问题。 面试官 - Java 是否支持多重继承? 我 - 不 面试官 - Java 中的每个类都扩展了类 Object(类 Object 除外),如果我们从外部扩展一个类,例如 Cl
我目前正在实现我的第一个 GWT 应用程序,我只是有一个快速的问题,关于在创建复杂的自定义小部件时 Extends Composite 和 Extend a specified widget 之间的区
使用 Observable 扩展 Object 和应用于以下类的扩展 Observable 之间有什么区别。 当应用程序运行时,结果是一样的。 library models; import 'pack
我制作了一个类装饰器,我想限制这个装饰器只能应用于某些类,所以我这样做了: @decorator() class A { foo:string; } @decorator() class B
在这个例子中: import java.util.*; public class Example { static void doesntCompile(Map> map) {} st
注意:这个问题与 Enum 无关,所以它不是重复的。Enum 被迫只与自身比较,因为编译器生成类型参数,而不是因为 java 递归类型参数。 我试图找到将类声明为的优势: public class S
我是 Java 的新手,正在尝试从 Java 泛型和集合一书中理解以下奇怪的语法。(我广泛使用 C++ 模板,因此可以声称了解泛型编程的基础知识和可能的陷阱): interface Collect
注意:这个问题与 Enum 无关,所以它不是重复的。Enum 被迫只与自身比较,因为编译器生成类型参数,而不是因为 java 递归类型参数。 我试图找到将类声明为的优势: public class S
有人知道是否可以延长 child Blade 吗? 我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列
我是一名优秀的程序员,十分优秀!