Add external javascript files with blade engine code in Laravel 5.x?

两盒软妹~` 提交于 2020-12-16 04:17:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33048567/add-external-javascript-files-with-blade-engine-code-in-laravel-5-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!