在Laravel 5.x中添加带有刀片引擎代码的外部javascript文件? [英] Add external javascript files with blade engine code in Laravel 5.x?
问题描述
我有多个Javascript文件,其中许多是由laravel模板引擎准备的.例如不适使用:
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
一切正常,生病时将javascript代码直接包含到我的filename.blade.php模板中.
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>
因此,刀片引擎未对其进行处理.病态找到了L4的一些解决方案,但我对它们并不满意.例如,您应该将您的javascript文件命名为javascript.blade.php并以这种方式包含它-但是我的IDE认为它是PHP文件并对其格式设置完全错误.此处的另一种解决方案是在添加您的(在此示例中为组)之前,将其写入,例如:
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.
有解决方案吗?
谢谢.
推荐答案
假设您具有以下外部JS脚本(还包括一些刀片引擎代码):
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 (,但请记住将整个脚本代码放在脚本标签中!)
现在可以通过以下方式将此文件包含在其他刀片文件中:
Now include this file inside your other blade file in the following way:
@include('someViewFolder.first'),前提是您已将 first.blade.php 放在" someViewFolder "文件夹中.
@include('someViewFolder.first') assuming you had put the first.blade.php inside 'someViewFolder' folder.
我希望这会有所帮助,干杯.
I hope this helps, Cheers.
这篇关于在Laravel 5.x中添加带有刀片引擎代码的外部javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!