vash template engine Node.js

www.igi‮aeditf‬.com

Vash is a template engine for Node.js that is designed to be simple and lightweight. It uses a syntax that is similar to Razor, a popular template engine for Microsoft's ASP.NET framework.

Here's an example of how to use Vash in Node.js:

  1. Install Vash using npm:
npm install vash
  1. Create a simple Vash template called index.vash:
<!DOCTYPE html>
<html>
  <head>
    <title>@model.pageTitle</title>
  </head>
  <body>
    # @model.pageTitle
    
@for(var i = 0; i < model.authors.length; i++) {
        - @model.authors[i]

      }


  </body>
</html>
  1. Create a Node.js file called app.js that uses Express to render the template:
const express = require('express')
const app = express()
const vash = require('vash')

const compiledFunction = vash.compileFile('index.vash')

const authors = ['Alice', 'Bob', 'Charlie']
const pageTitle = 'My Blog'

app.get('/', (req, res) => {
  res.send(compiledFunction({authors, pageTitle}))
})

app.listen(3000, () => {
  console.log('Server started on port 3000')
})

In this example, we first import the vash module and compile the index.vash template using vash.compileFile(). We then define an array of authors and a page title, and use Express to serve the template to the client.

When the client requests the root URL, Express renders the Vash template and sends the resulting HTML to the client.

Note that this is just a simple example - Vash supports a variety of features, including conditionals, loops, and layouts, as well as custom helpers and filters. You can refer to the Vash documentation for more information on how to use this flexible and powerful template engine.