[英]How to share data for all pages with help @extends @section @yield in Laravel?
How to share data for all pages with help @extends
@section
@yield
in Laravel? 如何在Laravel中通过帮助@extends
@section
@yield
共享所有页面的数据? For example, there is 1 controller. 例如,有1个控制器。 It displays all news, all comments, all users. 它显示所有新闻,所有评论,所有用户。 This controller inherits the main layer ( layouts.app
). 该控制器继承主层( layouts.app
)。 How do I make all other pages inherit this data? 如何使所有其他页面继承此数据?
How to make the other.blade.php page have data from the main.blade.php page? 如何使other.blade.php页面具有来自main.blade.php页面的数据? A simple example: 一个简单的例子:
// layouts.app.blade.php
<!DOCTYPE html>
<head>
...
</head>
<body>
@yield('one')
@yield('two')
@yield('content')
</html>
//main.blade.php
@extends('layouts.app')
@section('one')
// dynamic content
@endsection
@section('two')
// dynamic content
@endsection
//other.blade.php
@extends('layouts.app')
@section('content')
// dynamic content
@endsection
Answering to your question about how to share data on components you can read the Laravel docs about blade templating: 在回答有关如何在组件上共享数据的问题时,您可以阅读有关刀片模板的Laravel文档 :
Passing Additional Data To Components 将其他数据传递到组件
Sometimes you may need to pass additional data to a component. 有时您可能需要将其他数据传递给组件。 For this reason, you can pass an array of data as the second argument to the @component directive. 因此,您可以将数据数组作为第二个参数传递给@component指令。 All of the data will be made available to the component template as variables: 所有数据将作为变量提供给组件模板:
@component('alert', ['foo' => 'bar']) ... @endcomponent @component('alert',['foo'=>'bar'])... @endcomponent
Example: 例:
@extends('view', ['data_1' => 1, 'data_2' => 2])
OR 要么
@include('view', ['title'=> 'Example of include'])
About your blade structure question I would do like this: 关于您的刀片结构问题,我想这样做:
// layouts.app.blade.php
<!DOCTYPE html>
<head>
...
</head>
<body>
@yield('one')
@yield('two')
@yield('content')
</html>
//main.blade.php
@extends('layouts.app')
@section('one')
// dynamic content
@endsection
@section('two')
// dynamic content
@endsection
@include('other.blade.php', ["data" => 'your_data'])
//other.blade.php
@section('content')
// dynamic content
@endsection
The problem on this is depending on your App the blade structure can vary a lot, so maybe is so extensive question to answer here. 这个问题取决于您的App,刀片结构可能有很大不同,因此在这里回答的问题可能如此广泛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.