我的 javascript 文件中的 Laravel 4 刀片语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18074771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 blade syntax within my javascript files
提问by Mwirabua Tim
My home page had some inline javascript that was mixed up with some blade syntax e.g.
我的主页有一些内联 javascript 与一些刀片语法混合,例如
<script type="text/javascript">
@if(Auth::user())
if(path.indexOf('/user/' + {{Auth::user()->id}} ) != -1) {
$( "#tabs" ).tabs();
};
@endif
</script>
It worked until I wanted to move the javascript to an external file.js. I got error whenever blade syntax was added. Is there a way I can fuse blade syntax in my javascript files.js? I tried renaming to file.blade.js with no luck...
它一直有效,直到我想将 javascript 移动到外部 file.js。每当添加刀片语法时,我都会出错。有没有办法在我的 javascript files.js 中融合刀片语法?我尝试重命名为 file.blade.js 没有运气...
采纳答案by Trying Tobemyself
you can try this save your javascript file in app/views
folder and rename it to xxx.blade.php
, yes .blade.php
because Blade Enginewill parse it only if its .blade.php
and use @include('your javascript filename')
to include the javascript file parsed by Blade, it will work.
您可以尝试将您的 javascript 文件保存在app/views
文件夹中并将其重命名为xxx.blade.php
,是的,.blade.php
因为Blade Engine只会解析它.blade.php
并用于@include('your javascript filename')
包含Blade 解析的 javascript 文件,它会起作用。
回答by Brandon Romano
Although the accepted solution will work, this is a most definitely an antipattern.
尽管公认的解决方案会起作用,但这绝对是一种反模式。
If I saw this not being the one who wrote it, I would be extremely confused to what's going on.
如果我看到这不是写它的人,我会对发生的事情感到非常困惑。
My suggestion is in your PHP file, have a block, which gets all of the values that you'll need in your external files, then call the external files.
我的建议是在你的 PHP 文件中,有一个块,它获取你在外部文件中需要的所有值,然后调用外部文件。
So in your PHP file you would have something like:
所以在你的 PHP 文件中,你会有类似的内容:
<script>
var userID = "{{ Auth::user()->id }}";
var isUser = "{{ Auth::user() }}"
</script>
{{ HTML::script('path/to/js/file.js') }}
And in your javascript file:
在你的 javascript 文件中:
if(isUser)
{
if(path.indexOf('/user/' + userID ) != -1) {
$( "#tabs" ).tabs();
};
}
回答by Luco
I was doing the same than @BrandonRomano, but I found a better approach. Sending directly the value from PHP to JS vars using: PHP-Vars-To-Js-Transformer
我和@BrandonRomano 做的一样,但我找到了更好的方法。使用PHP-Vars-To-Js-Transformer将值从 PHP 直接发送到 JS 变量
PHP:
PHP:
JavaScript::put([
'foo' => 'bar',
'user' => User::first(),
'age' => 29
]);
JS:
JS:
console.log(foo); // bar
console.log(user); // User Obj
console.log(age); // 29
You can set a namespace like:
您可以设置一个命名空间,如:
console.log(server.foo)
回答by Andreyco
You are outputing string from PHP, so you have to enclose that string in '
您正在从 PHP 输出字符串,因此您必须将该字符串包含在 '
<script type="text/javascript">
@if(Auth::user())
if(path.indexOf('/user/' + '{{Auth::user()->id}}' ) != -1) {
$( "#tabs" ).tabs();
};
@endif
</script>