How to construct dynamic variable NAMES?

北城以北 提交于 2019-12-12 17:02:20

问题


I want to loop over data, and create dynamic maps that I can later push more data into. For example:

#foreach ($item in ["bob","john","andy"])
  #set(${item}_map = {})
#end

So that later I can do this:

$!bob_map.put("${foreach.count}", "${some_data}")

回答1:


I would do:

#foreach ($item in ["bob","john","andy"])
  <div id="${item}_map" ></div>
#end

So later you can do in JS:

<script>
var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('bob_map'), {
    center: {lat: -34.397, lng: 150.644},
    zoom: 8
  });
}

</script>


来源:https://stackoverflow.com/questions/33283748/how-to-construct-dynamic-variable-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!