Laravel Asset Pipeline Conditional JavaScript

Since the Asset Pipeline package for Laravel does not allow you to conditionally add scripts / style sheets to a page, what is the best way to do this?

I have one JavaScript file, which is a collection of files combined together that are included on each page, providing us with a library file. Now I need to add another JavaScript file for certain pages (they all use the same template).

My current plan is to expand BaseControllerto add an array of scripts / styles that can be dynamically bound to the template if there are any elements. But of course, is there a better way that is part of Asset Pipeline packages?

+3
source share
1 answer

It does not use asset-pipeline, but I use this directive @parentin Blade templates.

On my site, I have several jsand several cssthat I need on every page (for example, Twitter bootstrap), and I have Blade layoutone that includes them.

Then on some pages (my application is an analytic application) I need to enable special resources for the page (for example, a graphics library).

Therefore, on my layout there are:

@section('js')
{{ HTML::script('js/a.js') }}
@show

@section('css')
{{ HTML::style('css/a.css') }}
@show

And then on a specific 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 subpages in my application are very specific as to what additional assets they need, so controlling them in this way is very effective.

+7
source

All Articles