2015-02-05 68 views
0

我用的烧瓶的Jinja2和MapBox上,其中包括使用从模型数据导出GeoJSON的在地图上绘制数据的项目。如何这个例子加载:瓶和Jinja2的url_for错误 - 串联JSON对象到url_for

$.getJSON("{{ url_for(".geojson") }}", function(data) { 
    var geojson = L.geoJson(data, { 
     onEachFeature: function (feature, layer) { 

    //do stuff 

     } 
    }); 
    markers.addLayer(geojson); 

    var map = L.map('map', {maxZoom: 9, minZoom: 3}).fitBounds(markers.getBounds()); 
    baseLayer.addTo(map); 
    markers.addTo(map); 

用我的JS在此JSON数据的一个例子:

var feature = e.layer.feature; 
//print item name 
console.log(feature.properties.name) 
//print item latitude 
console.log(feature.properties.latitude) 
//print item category info 
console.log(feature.properties.category.name) 

这个伟大的工程。我的数据集现在已扩展为包含图像网址(例如09379_580_360.jpg),并且图像本身位于static/images/eol文件夹中。我想包括这些作为一个DIV,由本人通过动态JS像这样设置内的图像...

var commoncontent = '<div class="panel-heading"><h3>'+feature.properties.name+'</h3></div>' 
$('#common').html(commoncontent) 

然而,当我试图将我的图像数据连接成神社的url_for ...

var commoncontent = '<div><img src="{{ url_for("static", filename="images/eol/thumbs/big/'+feature.properties.category.localimageurl.jpg+'") }}"></div>' 

...我得到这个错误在我的控制台

GET http://127.0.0.1:5000/static/images/eol/thumbs/big/feature.properties.category.localimageurl.jpg 404 (NOT FOUND) 

我知道feature.properties.category.localimageurl是正确的,因为它打印到我的控制台时,我CONSOLE.LOG()它。然而,我不知道为什么解释者直接把它作为一个字符串,而不是连接它?

回答

1

feature是JavaScript对象。 Jinja无法访问这些内容;它运行在服务器上,而你的JavaScript运行在客户端上。呈现模板时,feature不存在。你将需要处理与JavaScript的连接。

var commoncontent = '<div><img src="{{ url_for("static", filename="images/eol/thumbs/big/") }}' + feature.properties.category.localimageurl.jpg + '"></div>'