如何在脚本文件中使用Laravel Blade? [英] How to use Laravel Blade in a script file?

查看:109
本文介绍了如何在脚本文件中使用Laravel Blade?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此教程和Laravel 5制作商店定位器应用程序。在这些问题中的人此处这里似乎使用@foreach循环和其他刀片模板语言运行它们的纬度/经度坐标。他们是怎么做到的?



我基本上不知道如何在我的地图代码位于js文件时使用刀片循环坐标?这怎么可能?我正在做一些完全错误的事情吗?



我用带有以下代码的js文件(maps.js)显示我的地图:

 函数初始化(){

var map_canvas = document.getElementById('map');

//初始化地图
var map_options = {
center:location,
zoom:10,
mapTypeId:google.maps.MapTypeId.ROADMAP

var map = new google.maps.Map(map_canvas,map_options)

//将所有位置放入数组
var locations = [
@foreach($ articles as $ article)
[{{$ article-> lat}},{{$ article-> lng}}]
@endforeach
]; (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); //可能没有必要,因为你将地图设置在

以上

PS:如果有人按照Laravel 5的教程进行操作,我将非常感激这部分的任何信息:输出XML与PHP。

解决方案

在外部JavaScript文件中无法使用Blade模板。



Laravel只能将数据传递给视图/模板; Javascript文件是外部加载的,因此App数据不会传递给它们。



为了解决这个问题,您需要创建< script> ; 在您的Blade模板文件中的标签: ,view.blade.php - }}

< script type =text / javascript>

//将所有位置放入数组
var locations = [
@foreach($ articles as $ article)
[{{$ article-> lat }},{{$ article-> lng}}],
@endforeach
];

//注意:我已经添加了一个逗号来分隔数组中的每个数组。
//因为经线和长线不是整数,所以也需要引号。

< / script>

请确保此代码段 包含 maps.js 文件的代码。如果您在< head> 标签中包含了 maps.js 文件,那么您需要将包含片段移动到页面底部。



然而,这是一个相当基本的解决方案。更好的方法是在初始化时使用AJAX从 maps.js 文件中检索数据。



这当然会要求您创建一个新的Controller方法和相应的路线,以处理请求并返回所需的数据。


I am trying to make a store locator app using this tutorial and Laravel 5. People in these questions Here and Here seem to be using @foreach loops and other blade templating language to run through their lat/long coordinates. How are they doing that?

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?

I am showing my map with a js file (maps.js) that has the following code:

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.

PS: If anyone has followed this tutorial with Laravel 5 I would be grateful for any info on this part: Outputting XML with PHP.

解决方案

There is no way to use Blade templating within an external Javascript file.

Laravel can only pass data to a view/template; Javascript files are loaded externally and therefore App data is not passed to them.

In order to get around this, you need to create <script> tags within your Blade template file:

{{-- 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 before the 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.

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.

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.

这篇关于如何在脚本文件中使用Laravel Blade?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆