简体   繁体   English

有没有办法我可以在Laravel刀片中单独包​​含部分?

[英]Is there a way I can separately include section in Laravel blade?

When extending a view in laravel blade, to have multiple places of inserting, it can be done as something like this: 在laravel刀片中扩展视图时,可以有多个插入位置,可以这样进行:

<html>
<head>
    <title>This is app/view/layout/default.blade.php</title>
    {{ HTML::script('js/jquery-2.1.3.min.js') }}
    {{ HTML::script('js/angular.js') }}
    @yield('extra_libraries')
</head>
<body>
    <div class="left-menu-bar">
        @yield('left-menu-bar')
    </div>
    <div class="main-content">
        @yield('main-content')
    </div>
</body>
</html>

Then, when using the layout, 然后,当使用布局时,

@extend ("layout.default")
@section('extra_libraries')
    {{ HTML::script('js/underscore.js') }}
@stop
@section('left-menu-bar')
    <a href="http://www.google.com/">testing button</a>
@stop
@section('main-content')
    testing
@stop

What I want is a similar function when I am not extending but to include another blade into my current blade. 当我不扩展但要在当前刀片中包括另一个刀片时,我想要的是类似的功能。 ie something like this: 即这样的事情:

<html>
<head>
    <title>This is app/view/layout/default.blade.php</title>
    {{ HTML::script('js/jquery-2.1.3.min.js') }}
    {{ HTML::script('js/angular.js') }}
</head>
<body ng-app="myTestingApp">
    <div ng-controller="testing">
        @include('components.componentA.htmlComponent')
        @include('components.componentB.htmlComponent')
    </div>
    <script>
    var app = angular.module('myTestingApp', []).controller("testing", testingController);
    function testingController($scope) {
        @include('components.componentA.angularCode')
        @include('components.componentB.angularCode')
    }
    </script>
</body>
</html>

where componentA should be a blade file containing both anguarCode and htmlComponent. 其中componentA应该是包含anguarCode和htmlComponent的刀片文件。 I know I can separate them into 2 files, but I want to see if there is a way to put them in the same file as they are closely related. 我知道我可以将它们分成2个文件,但是我想看看是否有一种方法可以将它们放入紧密相关的同一文件中。 Is there default function in laravel blade to achieve this? laravel刀片中是否有默认功能来实现这一目标?

PS The example shows why I want to put them together, if you know angularjs. PS该示例说明了如果您知道angularjs,为什么要将它们放在一起。

to include another blade into my current blade 在我当前的刀片中包含另一个刀片

Well, to include another blade file inside an existing one, it is definitely the @include tag that comes to save the day. 好吧,要在现有文件中包含另一个刀片文件,肯定是@include标记可以节省一天的时间。 If it still doesn't satisfies your requirements, you can always write your own methods by extending Blade . 如果仍然不能满足您的要求,则可以始终通过扩展Blade来编写自己的方法。

However, the whole thing seems a little too complicated to me. 但是,整个事情对我来说似乎有点复杂。 Why don't you separate your JavaScript into different controllers and then use ng-view to call the corresponding HTML, as shown in that example at the bottom of the documentation. 您为什么不将JavaScript分成不同的控制器,然后使用ng-view调用相应的HTML,如文档底部的示例所示。

Not sure but you could try 不确定,但您可以尝试

...
<body ng-app="myTestingApp">
    <div ng-controller="testing">
        @yield('componentA-html')
        @yield('componentB-html')
    </div>
    <script>
    var app = angular.module('myTestingApp', []).controller("testing", testingController);
    function testingController($scope) {
        @yield('componentA-angularCode')
        @yield('componentB-angularCode')
    }
    </script>
</body>
@include('components.componentA')
@include('components.componentB')
</html>

And your componentA blade should be 并且您的componentA刀片应该是

@section('componentA-html')
   html for A
@endsection
@section('componentA-angularCode')
   angularCode for A
@endsection

the same way for compomentB blade compomentB刀片的相同方法

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

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