2

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]})
Theodor
  • 5,536
  • 15
  • 41
  • 55

2 Answers2

6

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>
Kyle Wollman
  • 99
  • 1
  • 7
-4

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.

Andrew Burgess
  • 1,765
  • 17
  • 19