Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars get access to data in script tag

I've got a plain handlebars template that I'm passing through puppeteer to generate a pdf. I'm able to access the data through the body normally but can't seem to do so in a JS script tag. My data looks something like this.

const context = { data: [ {..some data}, {...some data}, {...some data}] }

I'm trying to access it in my script tag like

const data = "{{ data }}"

but all I get back when I console log this in my script tag is

[object Object],[object Object],[object Object]

When I check the typeof, it comes back as a string.

like image 399
LTFoReal Avatar asked Dec 07 '25 12:12

LTFoReal


1 Answers

Solved it by making a simple helper to stringify the data. (I have my helpers in a helper object)

json: function(obj) {
    return JSON.stringify(obj);
  }

Then in my JS script I just call it with the triple brackets

const wedges = {{{ json data }}}
like image 110
LTFoReal Avatar answered Dec 12 '25 02:12

LTFoReal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!