1

I am using this code inside my PHP file:

layout.blade.php

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

The problem is abc will be different for different pages, so I need to change it to a variable (i.e. $js) to make the value dynamic.

Currently I am using:

{{ HTML::script('js/'.$js.'.min.js') }} to achieve this.

I was wondering whether this is the regular way to do this? Or is there any better way?

And I tried to use {{ HTML::script('js/'.$js.'.min.js') or ''}} to check the existence of $js, but it's not working. Is @if (isset($js)) my only solution?

Thanks.

2
  • how is $js set? do you scan directories for this? or just simple string assignment? Commented Apr 29, 2014 at 4:39
  • @kevinabelita using with method in routes.php i.e. ->with('js', 'index') Commented Apr 29, 2014 at 4:52

1 Answer 1

1

The way that you are using HTML::script is not horrible, and it is an acceptable way of getting the job done!

I would however recommend using a Form macro, if for any other reason then it just looks nicer when reading the template, as I try to avoid using as little PHP as possible in my views.

Inside your app/start.php file, enter something like this:

 Form::macro('javascript_file', function($js = null) {
    if ($js === null) return;

    return HTML::script( 'js/'.$js.'.min.js');
 }

Then inside your templates you can just go:

@if isset($js)
{{ Form::javascript_file($js); }}
@endif

Sadly there is no way to check if $js is set before calling javascript_file without it looking more ugly...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.