Vash Template Engine for Node.js with Express.js
Discover how to integrate the Vash template engine with Express.js in Node.js. This guide covers installation steps and basic usage for dynamic HTML generation, inspired by ASP.Net MVC's Razor syntax.
Vash, inspired by ASP.Net MVC's Razor Syntax, simplifies dynamic HTML generation in Node.js applications. This guide provides a step-by-step approach to integrating Vash with Express.js.
Installation
- Open your terminal and navigate to your project directory.
- Install Vash using npm:
Command
npm install vash --save
This command saves Vash as a dependency in your package.json
file.
Creating Vash Templates
- Create a folder named
views
to store your Vash templates. - Inside
views
, create a file namedindex.vash
with the following content:
index.vash
<!DOCTYPE html><br>
<html><br>
<head><br>
<title>@model.title</title><br>
</head><br>
<body><br>
<p>@model.content</p><br>
</body><br>
</html>
This template defines placeholders for title
and content
using @model
.
Integrating Vash with Express.js
- Create a file named
server.js
in your project directory. - Include the following code in
server.js
:
server.js
const express = require('express');
const app = express();
// Set Vash as the view engine
app.set('view engine', 'vash');
app.get('/', (req, res) => {
// Data for the template
const data = {
title: 'Vash Template Demo',
content: 'This is a dummy paragraph.'
};
// Render the index template with data
res.render('index', data);
});
const server = app.listen(5000, () => {
console.log('Server is running on port 5000');
});
This code sets Vash as the view engine for Express.js. The app.get
route handler renders the index.vash
template with the provided data object.
Running the Application
- In your terminal, run:
- Open http://localhost:5000 in your browser to see the rendered HTML content.
Command
node server.js
Layout Pages
Vash allows you to create layout pages with reusable elements like headers and footers. Here's an example:
- Create a file named
layout.vash
in theviews
folder: - Modify
index.vash
to extend the layout and inject content: - Update
server.js
to render the modifiedindex.vash
:
layout.vash
<!DOCTYPE html><br>
<html><br>
<head><br>
<title>@model.title</title><br>
</head><br>
<body><br>
<header><br>
<!-- Header content --><br>
</header><br>
@html.block('body')<br>
<footer><br>
<!-- Footer content --><br>
</footer><br>
</body><br>
</html>
This layout defines a body
block where dynamic content from individual pages can be injected.
index.vash
@html.extend('layout', function(model) {
@html.block('body', function(model) {
@model.title
@model.content
});
});
Here, @html.extend
specifies the layout page (layout.vash
). Within the body block, the actual content is defined.
server.js
// ... other code ...
app.get('/', (req, res) => {
res.render('index', { title: 'Vash Template Demo', content: 'This is a dummy paragraph.' });
});
Now, the rendered page will include the layout's header and footer along with the dynamic content from index.vash
.
Additional Notes
- Vash offers features like helpers, filters, and partials for advanced scenarios.
- Explore the official Vash documentation for detailed information and examples.
By following these steps and exploring Vash's capabilities, you can create efficient and maintainable web applications in Node.js with a familiar templating syntax.