From be4acf8d89967586009ecd268c62d448ba9c87cf Mon Sep 17 00:00:00 2001 From: Ankan Das Date: Tue, 24 Mar 2026 16:40:13 +0530 Subject: [PATCH] Revise template engines guide for Expressa Updated the guide on using template engines with Express, including details on setting up views and installing Pug. Signed-off-by: Ankan Das --- en/guide/using-template-engines.md | 50 +----------------------------- 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/en/guide/using-template-engines.md b/en/guide/using-template-engines.md index 9921ce5b7a..3639d171b3 100755 --- a/en/guide/using-template-engines.md +++ b/en/guide/using-template-engines.md @@ -1,52 +1,4 @@ ---- -layout: page -title: Using template engines with Express -description: Discover how to integrate and use template engines like Pug, Handlebars, and EJS with Express.js to render dynamic HTML pages efficiently. -menu: guide -order: 5 -redirect_from: "/guide/using-template-engines.html" ---- - -# Using template engines with Express - -A _template engine_ enables you to use static template files in your application. At runtime, the template engine replaces -variables in a template file with actual values, and transforms the template into an HTML file sent to the client. -This approach makes it easier to design an HTML page. - -The [Express application generator](/{{ page.lang }}/starter/generator.html) uses [Pug](https://pugjs.org/api/getting-started.html) as its default, but it also supports [Handlebars](https://www.npmjs.com/package/handlebars), and [EJS](https://www.npmjs.com/package/ejs), among others. - -To render template files, set the following [application setting properties](/{{ page.lang }}/4x/api.html#app.set), in the default `app.js` created by the generator: - -* `views`, the directory where the template files are located. Eg: `app.set('views', './views')`. -This defaults to the `views` directory in the application root directory. -* `view engine`, the template engine to use. For example, to use the Pug template engine: `app.set('view engine', 'pug')`. - -Then install the corresponding template engine npm package; for example to install Pug: - -```bash -$ npm install pug --save -``` - -
-Express-compliant template engines such as Pug export a function named `__express(filePath, options, callback)`, -which `res.render()` calls to render the template code. - -Some template engines do not follow this convention. The [@ladjs/consolidate](https://www.npmjs.com/package/@ladjs/consolidate) -library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express. -
- -After the view engine is set, you don't have to specify the engine or load the template engine module in your app; -Express loads the module internally, for example: - -```js -app.set('view engine', 'pug') -``` - -Then, create a Pug template file named `index.pug` in the `views` directory, with the following content: - -```pug -html - head +mahol pura wavyyy title= title body h1= message