you can set the respective property in app.locals or res.locals objects to true or. We’ll make use of axios as our HTTP client for the requests to the Random User API. Pug fully integrates with Express, a popular Node.js web framework.
#HOW TO USE REQ.LOCALS IN PUG TEMPLATE INSTALL#
The first is going to initialize our folder with a package.json, and the second will install the npm dependencies needed. Within your Express route, you can then render your Pug files by calling the res.render function with the path of the file (starting from the directory set by the app.set("views") option). In a folder of your choice, run the following commands: npm init -y npm install express pug axios. app.set("view engine", "pug") įurther, you must set the view directory of your app so that Express knows where to look for your Pug files (for compilation). However, you must set the view engine property of your Express application to pug. A scenario as described above produces compiled pug template function what references locals.data when it should reference is because the first locals is the var container object passed to the template function. The example below defines block scripts, block content.
![how to use req.locals in pug template how to use req.locals in pug template](https://shots.codepen.io/Samson-kiran-02/pen/poPWrdO-800.jpg)
Providing default content is purely optional, though. Pug blocks can provide default content, if appropriate. In a template, a block is simply a block of Pug that a child template may replace. If you are using Express in your application, you do not need to require("pug"). Template inheritance works via the block and extends keywords. Instead of overriding that default, we will. By default, express expects a folder containing templates to be called views. Before we introduce the pug syntax, let's first start a new project and see what we need to include.
![how to use req.locals in pug template how to use req.locals in pug template](https://incloprestaurant.files.wordpress.com/2019/07/open-sign.jpg)
![how to use req.locals in pug template how to use req.locals in pug template](https://i.pinimg.com/originals/b6/82/0f/b6820fab5876b57f5b05c65fccb37d80.jpg)
You can now use pug in your project through the standard require mechanism: const pug = require("pug") There are quite a few we can use with Node.js including ejs, but we will be using one called pug (formerly known as jade ).