node.js - nodeJS + Swig template passing variable to javascript -
is there way using express + swig template nodejs pass variables server side client side javascript? know can done in jade i'd rather stick template engine more closely resembles html. guys!
ok assume configure express consolidate.swig if not please read link (http://tinyurl.com/kcs8kvy). didn't find direct way pass variable values client javascript have found workaround.
for instance you're sending object, in route @ express:
app.get("/", function(req, res){ var myuser = { name: "obama" }; res.render("index", {user: myuser}); });
then in index.html file can create script tag:
<html>
<body> <script> var username = "{{user.name}}"; </script> <script src="whatever.js"></script> </body>
</html>
and in whatever.js file username variable available correct value. hope you.
Comments
Post a Comment