

Render will search for a template file called 'template.extension' in the views folder and pass the. To render a template you'll use the render function in side the response object in your express routes. P= petName // - page-a.pug extends layout. Express can be used several different templating engines, most engines will assume by default that all your templates are in a 'views' folder. Then create a further directory called html and a. Create a new directory named pug-examples and change into it. It's worth noting that because the foot block here is not redefined, it still outputs "some footer content" // - pet.pug Now that we’ve got Pug installed, let’s try it out. Now you can define several new blocks to override the corresponding "parent block" in the parent template. Now extend the layout by simply creating a new file and using an extension as shown below to indicate the path of the inherited template.


Pug blocks can provide a default content, which is optional, of course // - layout.pug html

A code block called a "block" can be overwritten and replaced by sub-templates. “Developing template engines for Express”.Pug supports template inheritance using block and extends keywords. To learn more about how template engines work in Express, see: The view is still re-rendered with every request even when the cache is on. Note: The view engine cache does not cache the contents of the template’s output, only the underlying template itself. When you make a request to the home page, the index.pug file will be rendered as HTML. You must specify the extension of the view file. Then create a route to render the index.pug file. set ( 'view engine', 'pug' )Ĭreate a Pug template file named index.pug in the views directory, with the following content: html Library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express.Īfter the view engine is set, you don’t have to specify the engine or load the template engine module in your app Įxpress loads the module internally, as shown below (for the above example). Some template engines do not follow this convention. Which is called by the res.render() function to render the template code. Express-compliant template engines such as Jade and Pug export a function named _express(filePath, options, callback),
