简体   繁体   English

Laravel:从 package 扩展子视图中的布局

[英]Laravel: extending a layout in a child view from a package

We have been converting our MVC contents into separate packages from our Laravel app.我们一直在将我们的 MVC 内容从我们的 Laravel 应用程序转换为单独的包。 We still keep our layouts in the actual application, but the problem is that the packages are not able to use the @extends('layouts.mainlayout') directive approach to templating where we can render the sections inside the layout like such: <title>App Name - @yield('title')</title> using the section directives.我们仍然在实际应用程序中保留我们的布局,但问题是包无法使用@extends('layouts.mainlayout')指令<title>App Name - @yield('title')</title>来模板化我们可以在哪里呈现布局内的部分,如下所示: <title>App Name - @yield('title')</title>使用部分指令。

Here is a snapshot of the design of the app:这是应用程序设计的快照:

Laravel Application Laravel 应用

Main layout aka "master" page of the entire application整个应用程序的主布局又名“主”页面

 resources/views/layouts/mainlayout.blade.php

Example of a view from the application using mainlayout.blade.php使用 mainlayout.blade.php 的应用程序视图示例

ex: resources/views/home/index.blade.php例如:资源/视图/主页/index.blade.php

 @extends('layouts.mainlayout')
 @section('title')
     Home 
 @stop

 @section('css') 
    @parent
   <style>
     .#alp{font-size: 12pt;}
   </style>
 @stop

@section('content')
<table id="alp" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
@stop

@section('custom_js')
<script>
$(document).ready(function() {
    var t = $('#alp').DataTable( {
                "responsive": true,
        "scrollX": true,
        "initComplete": function(){
            CTSA.showRecordCount(t);
            CTSA.Flags.initCompleteDt = true;
        },
        "fnDrawCallback":function(){
            if(CTSA.Flags.initCompleteDt)
                CTSA.showRecordCount(t);
        }       
    });
} );
</script>
@stop

NOT WORKING PROPERLY无法正常工作

Example from package using mainlayout.blade.php使用 mainlayout.blade.php 的 package 示例

ex: packages/testpackage/resources/views/test.blade.php例如:packages/testpackage/resources/views/test.blade.php

@extends('layouts.mainlayout.php')

 @section('title')
     Test Page 
 @stop

I can get the package to use the layout, but I cannot get it to use it the way it should be using it via @extends('layouts.mainlayout') .我可以让 package 使用布局,但我无法通过@extends('layouts.mainlayout')让它以应有的方式使用它。 Currently it basically just crams the test.blade.php view into the mainlayout.blade.php layout.目前它基本上只是将test.blade.php视图填充到mainlayout.blade.php布局中。

It basically uses the mainlayout.blade.php template like this:它基本上使用 mainlayout.blade.php 模板,如下所示:

resources/views/layouts/mainlayout.blade.php资源/视图/布局/mainlayout.blade.php

@section('content')
 Contents of view test.blade.php get crammed here
@stop

The good news is that your packages have access to the root directory of your laravel application by default!好消息是你的包默认可以访问你的 Laravel 应用程序的根目录!

When you create a package and publish a package in your Laravel Application/Project a fresh copy of the package resides in /vendor/package-name当您在 Laravel 应用程序/项目中创建包并发布包时,该包的新副本位于/vendor/package-name

The ACTUAL published package resides in your Laravel Application/Project in the normal locations just like if it was created from within the Application/Project!实际发布的包位于 Laravel 应用程序/项目中的正常位置,就像它是从应用程序/项目中创建的一样!

For instance,例如,

Your package's assets ie: css,js,images get published in the app's public folder您包的资产,即:css,js,images 发布在应用程序的公共文件夹中

Laravel/App infrastructure: Laravel/App 基础架构:

App
   -Http
        -Controllers
                -AppController
                -PackageController 
   -Listeners
   -Console
public
   -assets
         -images
   -css
   -js
   -package-name
         -assets
                -images
         -css
         -js
resources
   -views
         -layouts
                -masterLayout.blade.php
                -partials
         -package-name
                -packageChildView.blade.php 
                -package-partials

As you can see, all of your packages can get to the layouts just fine /resources/views/ !如您所见,您的所有包都可以很好地进入布局/resources/views/ You can also get to public/ by default.默认情况下,您还可以访问public/

When taking over a large/unstructured project make sure you do a search on the file in question because you may be in for a surprise to find out that there are multiple copies of a file and the file your working on and in question may be the wrong one!在接管大型/非结构化项目时,请确保对相关文件进行搜索,因为您可能会惊讶地发现一个文件有多个副本,而您正在处理和相关的文件可能是错了!

So in short,所以简而言之,

The package will have access to the layouts in the application.该包将可以访问应用程序中的布局。 When you publish your views they will look in two spots for the layouts/views by default .默认情况下,当您发布视图时,它们会在两个位置查找布局/视图

Laravel actually registers two locations for your views: the application's resources/views/vendor directory and the directory you specify. Laravel 实际上为您的视图注册了两个位置:应用程序的 resources/views/vendor 目录和您指定的目录。

when extend a layout, by default Laravel will use layout from Laravel project project/resources/views/layouts/sample_layout.blade.php扩展布局时,默认情况下 Laravel 将使用 Laravel 项目project/resources/views/layouts/sample_layout.blade.php

@extend('layouts.sample_layout')

If we want to use package layout, assume your package layout like this如果我们想使用 package 布局,假设您的 package 布局像这样

package_name/resources/views/layouts/pk_layout.blade.php

we have to use this我们必须使用这个

@extend('package_name::layouts.pk_layout')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM