Laravel 5中使用JavaScript的部分视图 [英] Partial views with JavaScript in Laravel 5

查看:42
本文介绍了Laravel 5中使用JavaScript的部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个部分刀片文件,所有这些文件都只需要一点JavaScript.理想情况下,我希望局部加载所需的JS,而普通的JS不会包含它.我知道您可以将它们全部加载到一个大JS文件中,但是我想知道是否有一种方法可以提供一种更优雅的解决方案.

I have multiple partial blade files that all require just a little bit of JavaScript. Ideally I'd like the partial to load the JS it needs and the normal JS wouldn't include it. I know you can load it all into one big JS file, but I am wondering if there is a way to have a more elegant solution.

我希望以下解决方案适用于多个文件(它只能用于一个文件,但不适用于n.由于刀片的渲染引擎,第一次使用部分刀片会遇到该问题).

I'd like the solution below to work for multiple files (it works for one, but not n. The first partial blade encounters is used due to blade's rendering engine).

test.page.blade.php

@extends('layouts.default')
@section('title', 'Poses')

@section('content')
    <div class="container">
        <div class="row">
            @include('test.partial.one')
            @include('test.partial.two')
        </div>
    </div>
@stop
@section('javascript')
    @include('test.partial.one')
    @include('test.partial.two')
@stop

partial test.partial.one.blade.php

@section('content')

    <h1>One</h1>

@stop

@section('javascript')

    <script>Scripts from one</script>

@stop

partial test.partial.two.blade.php

@section('content')

    <h1>Two</h1>

@stop

@section('javascript')

    <script>Scripts from two</script>

@stop

推荐答案

问题的解决方案是@parent指令,您必须在javascript部分中使用该指令.

The solution to your problem is the @parent directive, which you have to use in your javascript sections.

test.page.blade.php

test.page.blade.php

@extends('layouts.default')
@section('title', 'Poses')

@section('content')
    <div class="container">
        <div class="row">
            @include('test.partial.one')
            @include('test.partial.two')
        </div>
    </div>
@stop

@yield('javascript')

test.partial.one.blade.php

test.partial.one.blade.php

@section('content')
    <h1>One</h1>
@stop

@section('javascript')
    <script>Scripts from one</script>
    @parent
@stop

test.partial.two.blade.php

test.partial.two.blade.php

@section('content')
    <h1>Two</h1>
@stop

@section('javascript')
    <script>Scripts from two</script>
    @parent
@stop

进一步参考: http://laravel.com/docs/5.0/templates

这篇关于Laravel 5中使用JavaScript的部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆