Let's delve into templating, EJS (Embedded JavaScript), and how views work in Express.js:
In the context of backend development, templating refers to the practice of dynamically generating HTML content by combining data with predefined template structures. This is particularly useful for building web pages that display information from a server, like user profiles or product listings. Here's names of some popular template engines:
-
Handlebars: Minimalistic, uses
{{ ... }}
for variables, supports helper functions. -
Pug (Jade): Clean and concise syntax, uses indentation, whitespace-sensitive.
-
Mustache: Logic-less, uses
{{ ... }}
for variables and simple tags for control structures. -
Nunjucks: Syntax similar to Jinja2/Twig, supports template inheritance and macros.
-
EJS (Embedded JavaScript): Familiar JavaScript syntax, allows embedding JavaScript code.
-
Swig: Inspired by Django templates, clean syntax for variables and control structures.
-
Haml: Inspired by Ruby, uses indentation, concise, and Ruby-like.
-
Eco: Compiles CoffeeScript into HTML, concise and clean syntax.
-
Slim: Lightweight and concise for Ruby, uses indentation, compact templates.
-
Marko: High-performance, supports asynchronous rendering, developed by eBay.
Choose a template engine based on your project's needs and your familiarity with the syntax.
- For Dynamic Content In Webpage
- Code Reuseability
- Consistency
- Security (Helps to prevent XSS attacks)
- Server Side Rendering : In some cases, templates are essential for server-side rendering (SSR). SSR is a technique that generates HTML on the server and sends it to the client, improving performance and SEO. Templates play a pivotal role in rendering HTML on the server before sending it to the browser.
- Data Handling
EJS is a popular template engine for JavaScript that allows you to embed JavaScript code directly into your HTML templates. It makes creating dynamic content easier by letting you mix HTML and JavaScript seamlessly.
In Express.js, views are the templates used to generate the HTML content that will be sent to the client's browser. They are responsible for presenting data from the server to the user. Views allow you to keep your HTML and presentation logic separate from your application logic.
In Express, the views are stored in a directory, typically named "views." This directory contains your template files (EJS files) that define how your web pages should look. Express expects this directory to be present, but you can customize its location if needed.
When you use EJS as your template engine, you can embed JavaScript directly into your HTML templates. This makes it easy to inject dynamic data into your web pages. EJS uses special tags to execute JavaScript code within your HTML.
```javascript
//Set the template engine as EJS
app.set("view engine","ejs");
//Setting the ejs files folders name by default views to custom-folder-name (viewsss)
//and access EJS files from anywhere if server is starting outside the directory
app.set("views",path.join(__dirname,"/viewsss"));
```
Here's a basic example of how you might use EJS in Express.js:
-
Create an EJS file in your "views" directory, e.g.,
profile.ejs
. -
In
profile.ejs
, you can embed JavaScript code using EJS tags like<% %>
. For example: html -
In your route handler, render the EJS template and pass data: javascript app.get('/profile', (req, res) => { const username = 'John'; // Replace with data from your application res.render('profile', { username }); });
-
When a user visits
/profile
, Express will render theprofile.ejs
template with the provided data.
While it's not compulsory to name your views in a certain way, it's a common practice to use meaningful names that reflect the purpose of the template. For example, profile.ejs
might represent a user's profile page.
In essence, views and EJS work together to dynamically generate HTML content that is sent to users' browsers. EJS tags allow you to inject data and logic directly into your templates, making your web pages come alive with dynamic content.
These are practical EJS syntax and code examples that can be valuable for your web development projects.
<% if (user.isAdmin) { %>
<p>Welcome, Admin!</p>
<% } else { %>
<p>Welcome, User!</p>
<% } %>
<ul>
<% users.forEach(function(user) { %>
<li><%= user.name %></li>
<% }); %>
</ul>
<h1><%= pageTitle %></h1>
<p><%- user.description %></p>
<%- include('header.ejs') %>
<p>Formatted Date: <%= formatDate(user.createdAt) %></p>
<ul>
<% for (let key in user) { %>
<li><%= key %>: <%= user[key] %></li>
<% } %>
</ul>
<%# This is a comment %>
app.get('/profile', (req, res) => {
const userData = { username: 'JohnDoe', age: 30 };
res.render('profile', { user: userData });
});
<%- include('header.ejs', { pageTitle: 'Home Page' }) %>
{ pageTitle: 'Home Page' }: This is an optional object that you can pass as a second argument to the include function. It allows you to pass data or variables to the included file. In this example, you're passing a variable called pageTitle with the value 'Home Page' to the 'header.ejs' file.
- In short, use this method when you want to include a file and with some variables which you can use them in that file.
These examples cover fundamental EJS syntax and code patterns that are commonly used in web development projects. EJS is known for its simplicity and compatibility with JavaScript, making it a convenient choice for dynamic content rendering.
Feel free to explore EJS's documentation and experiment with creating dynamic views in your Express.js applications!
When we want to add some static files in our templates then we use it, those static files can be css files, images, videos and other files.
Express Static Middleware:
express.static
is a built-in middleware function in Express.js.- It's used to serve static files like HTML, CSS, images, and client-side JavaScript to web clients (usually web browsers).
- Static files are files that don't change often and are the same for all users (e.g., your website's logo, styles, or JavaScript files).
How it Works:
- When you use
express.static
, you provide a folder path where your static files are located. - Express will automatically look for files in that folder when a request is made for a specific URL.
- If a file with a matching name is found, Express sends that file to the client.
Example:
Imagine you have a folder called "public" that contains your website's CSS, images, and JavaScript files. You can use express.static
to serve these files to clients:
app.use(express.static('public'));
//or
app.use(express.static(path.join(__dirname, 'public')));
- Now, when someone visits your website, if they request a file like
styles.css
in their browser, Express will automatically find and send thestyles.css
file from the "public" folder.
Benefits:
- Using
express.static
makes it easier to serve your static files without writing individual route handlers. - It improves performance by allowing browsers to cache static files, reducing the need to repeatedly request the same files.
- It's a common practice for delivering resources that don't change often.
Important Note:
- Be cautious when using
express.static
to serve sensitive files, as they will be accessible to anyone who knows the file's URL. Avoid serving confidential data this way.
In simple terms, express.static
is like a waiter at a restaurant who brings you specific dishes from the kitchen (your static files) without you having to request each dish separately. It's a convenient way to deliver resources to web clients while focusing on building other dynamic parts of your website using Express.js.