node.js - How can I use data passed into jade template in the views javascript? -
rendering data template res.render('account/pets/allpets', { petmap, title: 'all pets' })
have tried 2 thing make work
a)
var neighborhoods = []; (var = 0; < petmap.length; i++) { neighborhoods.push(new google.maps.latlng(petmap[i].location.loc[0],pet.location.loc[1])) }
b)
var neighborhoods = [ each pet in petmap new google.maps.latlng(petmap[i].location.loc[0],pet.location.loc[1]) ]
assuming patmap
javascript object, required few hacky things make work. pug renders serverside, while javascript on client, cannot directly access pug variable in js. can convert js object string when pug compiles, parse json when client executes.
var petmap = json.parse(("#{json.stringify(petmap)}").replace(/"/g, '"')); /* let me break down in terms of execution "#{json.stringify(petmap)}" -> executes server side, converts #{} accesses pug variable petmap, has been converted object .replace(/"/g, '"') -> replaces quote escape sequences (something html security) quotes json can parsed json.parse -> parses json stringified json server sent client */
Comments
Post a Comment