The title= and h1= are our tags and title and message are keys in which Express will use to get the values from in the object that we’ll pass to res.render. Inside the views folder, create an index.pug and then put: html head title= title body h1= message Since we chose to use Pug as our template engine, the template files should have the. views folder to be our templates folder. We can use the res.render method to render our variables to our templates.įirst, we create the views folder to store our templates as we set the. The command above will install pug view engine for us to use. To install the template engine library, we can run something like the following: npm install pug -save It’ll set the templates folder to the views directory of our app.Īlso, we can set the view engine we want to use by setting the view engine property as follows: app.set('view engine', 'pug') To set the templates folder, we can set the views property by writing the following: app.set('views', './views')
![send data to pug template express send data to pug template express](https://4.bp.blogspot.com/-iReEjy4aj6U/Wh87nCQ0VqI/AAAAAAAAAA8/ErvL7fnBK0AnmqhYFT1SV36zs83wQHd1ACLcBGAs/s320/laptop_repair_vancouver.jpg)
To render template files, we can set the application settings.
![send data to pug template express send data to pug template express](https://present5.com/presentation/196a0e275ba81eab325be601e4e5221b/image-13.jpg)
The default for the Express application generator is Jade, but we can use the other ones listed as well. We can use popular template engines with Express, like Pug, Mustache, and EJS.
#Send data to pug template express how to#
In this article, we’ll look at how to set the template engine of our app, and returning HTML responses. Templates are convenient and we can use them in a much more flexible way than HTML strings. To render an HTML response with an Express route, we should return it as a template with the variable interpolated rather than return it directly as an HTML string.