Javascript 如何在脚本文件中使用 Laravel Blade?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33489931/
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
How to use Laravel Blade in a script file?
提问by thomas jaunism
I am trying to make a store locator app using this tutorialand Laravel 5. People in these questions Hereand Hereseem to be using @foreach loops and other blade templating language to run through their lat/long coordinates. How are they doing that?
我正在尝试使用本教程和 Laravel 5制作商店定位器应用程序。这里和这里这些问题中的人似乎正在使用 @foreach 循环和其他刀片模板语言来运行他们的经纬度坐标。他们是怎么做的?
I basically do not know how to loop through coordinates using blade when my map code is in a js file? How is that possible? Am I doing something totally wrong?
当我的地图代码在 js 文件中时,我基本上不知道如何使用刀片遍历坐标?这怎么可能?我做错了什么吗?
I am showing my map with a js file (maps.js) that has the following code:
我正在使用具有以下代码的 js 文件 (maps.js) 显示我的地图:
function initialize() {
var map_canvas = document.getElementById('map');
// Initialise the map
var map_options = {
center: location,
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
// Put all locations into array
var locations = [
@foreach ($articles as $article)
[ {{ $article->lat }}, {{ $article->lng }} ]
@endforeach
];
for (i = 0; i < locations.length; i++) {
var location = new google.maps.LatLng(locations[i][0], locations[i][1]);
var marker = new google.maps.Marker({
position: location,
map: map,
});
}
// marker.setMap(map); // Probably not necessary since you set the map above
}
But obviously that gets stuck on the @foreach line.
但显然这会卡在@foreach 行上。
PS: If anyone has followed this tutorial with Laravel 5 I would be grateful for any info on this part: Outputting XML with PHP.
PS:如果有人使用 Laravel 5 学习了本教程,我将不胜感激这部分的任何信息:使用 PHP 输出 XML。
回答by heisian
There is no way to use Blade templating within an external Javascript file.
无法在外部 Javascript 文件中使用 Blade 模板。
Laravel can only pass data to a view/template; Javascript files are loaded externally and therefore App data is not passed to them.
Laravel 只能向视图/模板传递数据;Javascript 文件是从外部加载的,因此应用程序数据不会传递给它们。
In order to get around this, you need to create <script>
tags within your Blade template file:
为了解决这个问题,您需要<script>
在 Blade 模板文件中创建标签:
{{-- Code in your template file, e.g., view.blade.php --}}
<script type="text/javascript">
// Put all locations into array
var locations = [
@foreach ($articles as $article)
[ "{{ $article->lat }}", "{{ $article->lng }}" ],
@endforeach
];
// NOTE: I've added a comma which will be needed to delimit each array within the array.
// Quotes will also be needed since lat and long are not integers.
</script>
Make sure that this snippet comes beforethe code for including your maps.js
file. If you've inlcuded the maps.js
file within your <head>
tags, you're going to need to move that inclusion snippet down to the bottom of the page.
确保此代码段位于包含文件的代码之前maps.js
。如果您已将maps.js
文件包含在<head>
标签中,则需要将该包含代码段移至页面底部。
This is a rather rudimentary solution, however. A better way would be to use AJAX to retrieve the data from within your maps.js
file upon initialization.
然而,这是一个相当基本的解决方案。更好的方法是maps.js
在初始化时使用 AJAX 从文件中检索数据。
This would, of course, require you to create a new Controller method and corresponding route that can handle the request and return the required data.
当然,这需要您创建一个新的 Controller 方法和相应的路由来处理请求并返回所需的数据。
回答by The Alpha
You can create a JavaScript
object from the Eloquent
object, for example take a look at the following code:
您可以JavaScript
从Eloquent
对象创建一个对象,例如看看下面的代码:
// index.blade.php
<script>var userObj = {{ $authUser or 'undefined' }}</script>
This is a blade
view and I'm just loading the view
and passing the collection
to the view using something like this (Using a view composer):
这是一个blade
视图,我只是使用这样的东西(使用视图作曲家)加载view
并传递collection
给视图:
View::composer('layouts.master', function($view) {
$user = null;
if(Auth::check()) {
$user = Auth::user();
}
$view->with('authUser', $user);
});
So, in my view
I've $authUser
so I can convert it to JS
object easily like:
因此,在我的view
Ive 中$authUser
,我可以JS
轻松地将其转换为对象,例如:
<script>var userObj = {{ $authUser or 'undefined' }}</script>
So now, I can use it as a JS
object which is in userObj
variable (JavaScript). Check this article of minethat I've written nearly a year ago for Laravel-4
.
所以现在,我可以将它用作可变JS
对象userObj
(JavaScript)。检查这篇文章我的,我已经写了近一年前因Laravel-4
。
回答by James
In the samples you provided, they are using blade because they are in a blade file, and all JavaScript is between tags. It is not a .js filetype, so you can use blade language features this way.
在您提供的示例中,它们使用刀片是因为它们位于刀片文件中,并且所有 JavaScript 都在标记之间。它不是 .js 文件类型,因此您可以通过这种方式使用刀片语言功能。