问题
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