当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Including external resources through a view that extends a layout

问题描述:

I'm trying to learn Laravel by using it for one of my sites however I'm having trouble working something out.

I have a layout.blade.php file which holds all my global markup. Stuff like the header, footer etc. I'm also including some external stuff like stylesheets and scripts in <head> like this:

{{ HTML::script('js/bootstrap.min.js'); }}

However, there's some external stuff that I only want to load on certain pages. I'm including individual views from the header like this:

<div id="content">

@yield('content')

</div>

and in the views I'm extending the layout like so:

@extends('layout')

@section('content')

<h1>some content</h1>

@stop

How can I use a HTML::script type function to include stuff in my <head> from a view that extends layout.blade.php?

Thanks!

网友答案:

In your layout.blade.php, wrap your HTML::script calls in a @section() and @show tags, and simply extend them afterwards, in your child view, just like you did with section, but also using @parent to include the parent's content. Example:

layout.blade.php

@section('javascript')
    {{ HTML::script('js/jquery.js') }}
    {{ HTML::script('js/bootstrap.min.js') }}
@show

childview.blade.php

@extends('layout')

// ...

@section('javascript')
    @parent

    {{ HTML::script('js/customScript.js') }}
@stop
分享给朋友:
您可能感兴趣的文章:
随机阅读: