Shoulder.dev Logo Shoulder.dev

View Engine and Templating

View engines, such as EJS and Pug, are used to render HTML templates with dynamic content. They provide a way to separate the presentation logic (HTML structure and design) from the application logic (backend code) in web applications.

This separation of concerns is a key principle in web development, making code easier to maintain, test, and reuse. View engines enable you to create reusable templates that can be rendered with different data, ensuring consistent styling and layout across your application.

How it Works

View engines work by taking a template file, typically with a specific file extension (e.g., .ejs, .pug), and using the data provided by the backend to render the final HTML. The process involves:

  1. Template Creation: Creating templates using the chosen view engine syntax.
  2. Data Passing: Passing data from the backend (e.g., using Express.js res.render) to the template.
  3. Template Rendering: The view engine processes the template and replaces placeholders with the provided data.
  4. HTML Output: The rendered HTML is sent back to the client.

Why is View Engine and Templating Important?

  • Separation of Concerns: Separates presentation logic from application logic, making code easier to manage and maintain.
  • Reusability: Creates reusable templates that can be rendered with different data, reducing code duplication.
  • Dynamic Content: Allows for rendering dynamic content based on user input, database queries, or other factors.
  • Consistency: Enforces consistent styling and layout across your application.
  • Performance: Can improve performance by minimizing server-side rendering.

Example

// Example with EJS view engine:
      app.set('view engine', 'ejs');
      
      // Routes
      app.get('/', (req, res) => {
      const data = {
      title: 'Welcome to my site',
      message: 'This is a sample message',
      };
      res.render('index', data);
      });
      
      // index.ejs template
      <h1><%= title %></h1>
      <p><%= message %></p>
      

This code snippet demonstrates how to use the EJS view engine to render an HTML template (index.ejs) with dynamic content passed from the server.

Key Concepts

  • Template: A file that contains the HTML structure and placeholders for dynamic content.
  • View Engine: Software that interprets and renders templates into HTML.
  • Placeholders: Special syntax (e.g., <%= %>) in the template that is replaced with data from the server.
  • Data Passing: The process of sending data from the server to the view engine.
  • Rendering: The process of transforming the template into HTML with the data.

Choosing a View Engine

Several view engines are available, each with its own syntax and features. Some popular options include:

The choice of view engine depends on your project’s specific needs and your personal preferences.

Further Reading

Top-Level Directory Explanations

views/ - Subdirectory for storing the application’s templates, which are rendered as HTML responses.

Explanation