0

I have multiple Javascript files, and lots of them are prepared by the laravel template engine. For example ill use:

@if ($group_id !== 3)
    //do something here
@endif

Everthing is fine, when ill include this the javascript code directly into my filename.blade.php template.

But i would like to load all external with:

<script type="text/javascript">
    $.getScript("js/pages/filename.js");
</script>

And so its not processed by the blade engine. Ill found some solutions for L4, but i am not really happy with them. For example youll should name your javascript files like javascript.blade.php and include it in that way - but so my IDE thinks its an PHP file and formats it totally wrong. Other solution here is to write your (group in this example) before you include it, like:

<script type="text/javascript">
    var group = {!! $group_id !!}
    $.getScript("js/pages/filename.js");
</script>

I am still not really happy with that solution, i would like that all files should be processed as it was included directly in my blade-file.

Is there a solution to handle that?

Thanks in advance.

4
  • do you want add different files on each blade template? or the javascript file is loaded by validation using IF in blade? Commented Oct 9, 2015 at 23:56
  • Thanks for your answer! Yes, there are several blade template files. For each template ill got a javascript file i would like to add. There are lots of custom code for different groups (in datatables for example) so it would be nice to created a processed JS file for a specific group. So the "if" is directly in the JS File. Commented Oct 9, 2015 at 23:58
  • I'm not getting your idea clearly, but if you want to load each file depending of group, you can create a section for extra files and into this section manage each group, and load the specific file. If this not the idea tell me. Commented Oct 10, 2015 at 0:12
  • No thats not a good idea. For example. Youll got 1 large Datatable, but the columns are different for each group, so ill modify the columns directly in javascript with blade for each group. I dont want to create multiple JS files with beneath the same content (just for each group) - i prefer 1 file that is processed by the blade engine. Commented Oct 10, 2015 at 0:15

1 Answer 1

3

Lets say you have the following external JS script (including some blade engine code as well) :

first.js

First of all in order to use this script in any of the other blade file, rename the above file to the following:

first.blade.php (But remember to place the whole script code inside script tag!!)

Now include this file inside your other blade file in the following way:

@include('someViewFolder.first') assuming you had put the first.blade.php inside 'someViewFolder' folder.

I hope this helps, Cheers.

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

1 Comment

Hello. Thanks for your answer, the problem here is (as decribed above) that my IDE (PHPStorm) do not format correct javascript, if its a .php file. So it works fine, but is useless/hard to debug.

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.