Since the Asset Pipeline package for Laravel doesn't allow you to conditionally add scripts/stylesheets to the page, what is the best way of doing this?
I've got one JavaScript file which is a set of files concatenated together that are included on every page, providing us with a library file. What I now need to do is add another JavaScript file for certain pages (all of which use the same template).
My current plan is to extend the BaseController to add an array of scripts/styles that can be dynamically attached to the template if there are any items. But surely there is a better way that is part of the Asset Pipeline packages?
It doesn't make use of asset-pipeline, but the solution I use is the @parent directive in Blade templates.
On my site I have several js and several css that I need in every page (eg. Twitter bootstrap) and I have a Blade layout that includes those.
Then on certain pages (my app is an analytic app) I need to include page-specific assets (eg. a charting library).
On my layout, therefore, I have:
@section('js')
{{ HTML::script('js/a.js') }}
@show
@section('css')
{{ HTML::style('css/a.css') }}
@show
And then on a particular page, I have:
@extends('layouts.site')
@section('js')
@parent
{{ HTML::script('js/b.js') }}
@stop
@section('css')
@parent
{{ HTML::style('css/b.css')
@stop
This works great for me. The sub pages in my app are very specific about which extra assets they need so controlling them this way is very workable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With