Using Template Engines in Nodejs Express

Tram Ho

I’m TUAN, currently a Full-stack Developer in Tokyo.

If you find this Blog interesting, please give me a like and subscribe to support me.

Using Template Engines with Express

The Template engine allows you to use static Template files in your application. While the application runs, the Template engine replaces the variables in the Template file with actual value ​​and converts the Template into an HTML file that is sent to the Client . This approach makes it easier to design an HTML page.

Some popular Template engines that work with Express are Pug , Mustache , and EJS . Express application generator uses Jade as default, but it also supports a number of other applications.

See the Template Engines list (Express wiki) for a list of Template engines that you can use with Express . See also Compare JavaScript Template Engines: Jade, Mustache, Dust and more… .

Note : Jade has been renamed Pug . You can continue to use Jade in your application and it will work fine. However, if you want the latest update for the Template engine , you must replace Jade with Pug.

To display the Template file, set the following Application Setting Properties , set in the default app.js generated by the generator:

  • views , the folder containing the Template files. Ex: app.set('views', './views') . This defaults to the views folder in the root of the application.
  • view engine , Template engine to use. For example: app.set('view engine', 'pug') to use the Pug Template engine .

Then install the corresponding npm Template engine package; example to install Pug:

Fast compliant Template engines like Jade and Pug export a function named __express(filePath, options, callback) , which is called by the res.render() function to render the Template .

Some Template engines do not follow this convention. The Consolidate.js library follows this convention by mapping all the popular Node.js Template engines and thus works seamlessly in Express.

Once the view engine is set, you do not have to specify the engine or load the Template engine module in your application; Express loads the module internally, as shown below (for the example above).

Create a Template Pug file named index.pug in the views folder, with the following content:

Then create a Route to display the index.pug file. If the view engine property is not set, you must specify the extension of the view . If not, you can skip it.

When you request to '/' , the index.pug file will be displayed as HTML .

Note: The view engine cache does not cache the content of the output of the Template , only the underlying Template itself. The view is still re-rendered with every request even with cache enabled.

To learn more about how the Template engine works in Express , see: “Developing template engines for Express” .

Roundup

As always, I hope you enjoyed this article and learned something new.

Thank you and see you in the next posts!

If you find this blog interesting, please give me a like and subscribe to support me. Thank you.

Ref

Share the news now

Source : Viblo