Access handlebars properties from script tag

不打扰是莪最后的温柔 提交于 2019-12-12 05:08:29

问题


In a handlebars template, can I access a handlebar parameter inside a script tag like

<script>     
  var aList = {{list}}
</script>

The template is called from express with

 response.render('template', {list: [1, 2, 3]})

回答1:


You can use a hidden input in your html that contains the value you want and then get that using document.getElementById in the script tag.

   <input type = "hidden" id = "thingIWant" value = {{list}} />

   <script>
   var aList = document.getElementById("thingIWant").value;
   </script>



回答2:


Express is a back-end framework, so you won't be able to render front-end templates (that is, templates within <script> tags) from Express.

To use handlebars template with Express, you can use a package like express-handlebars; the documentation for that package will take you step-by-step through the setup process.



来源:https://stackoverflow.com/questions/37115630/access-handlebars-properties-from-script-tag

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