I have an express server where I render a template with express, using Pug templating engine. I of course am using Pug to pass and render my HTML with variables I pass in from my controller, however I need some of these variables accessible in my front end js code.

I am already successfully doing this by passing the string into the template and then rendering a script tag with the string I passed in from the controller, like this...

    window.rajax_visit_url= "#{ajax_add_visit_url}"
    window.unit_overview_url= "#{unit_overview_url}"

where ajax_add_visit and unit_overview_url are variables passed into the template from my controller. I then access these rendered variables in another js script loaded afterward.

However, this is very fragile and hard to maintain, especially when this has to be done in several templates. It's hard to tell and test whether these are being rendered correctly.

I want to know if there is a node library or even a more readable/maintainable process of doing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.