Monta
Yet another template language for Node.
Project status: early development
Not recommended for production, but should be okay for testing and playing around with. The basics work but expect bugs and frequent updates.
Why
- Not indentation-based
- Inheritance (extends & blocks)
- Pipe syntax
- Templates are HTML
How
Install the package in your project:
npm install monta
Create a template file:
<!-- my-template.mt -->${ foo }
Compile the template:
const Monta = ; const monta = ; const render = await monta;
Render your page:
const result = await ; console; // <p>bar</p>
Or do both in one go:
const result = await monta; console; // <p>bar</p>
You can also compile and render plain code:
monta;monta;
Use with Express
const express = ;const Monta = ; const monta = templateRoot: './views' ; const app = ;app; app;
Syntax
Monta templates are basically just HTML files. You can use any file
extension you like, but all project code and examples will use the
.mt
extension.
<!-- Print a variable -->${ myVar } <!-- Extend a base template file (must be the first thing in a file) -->${ extends('otherTemplate.mt') } <!-- Define a block -->${ define('blockName') } <!-- Define a block with default content -->${ define('blockName'): } Default content${ :end } <!-- Use a block -->${ block('blockName'): } Block content${ :end } <!-- Include another template file (in-place) -->${ include('otherTemplate.mt') } <!-- Array iteration -->${ myArr | foreach(): } <!-- Use ${ this } or ${ . } for the top-level object or variable in the current scope --> ${ this }${ :end } <!-- Control flow -->${ myVar | eq("foo"): } Variable is "foo"${ :end } ${ myVar | eq("foo"): } Variable is "foo"${ :else: } Variable is not "foo"${ :end } <!-- All control flow functions -->${ myVar | eq("foo"): } <!-- Equal (strict) -->${ myVar | neq("foo"): } <!-- Not equal (strict) -->${ myVar | lt("foo"): } <!-- Less than -->${ myVar | gt("foo"): } <!-- Greater than --> <!-- Checks if a value exists in an array, or a key in an object -->${ myVar | has("foo"): } <!-- String operations -->${ myVar | trim() }${ myVar | upper() }${ myVar | lower() }${ myVar | padLeft(6) }${ myVar | padRight(6) }
Plugins
Monta is designed to be modular and extendable through plugins.
To add a plugin to Monta, simply install it to the dependencies of your project. Monta should discover it automatically when it's initialised.
To add a plugin to Monta that it can't discover (e.g. because it's not
in the dependencies of the top-level package.json
file), you can add
the plugins
object to the Monta options.
const monta = new Monta({
plugins: {
'plugin-name': 'path/to/custom/plugin',
}
});
List of Plugins
If you've made a plugin, feel free to open a PR to add it to this list.
CLI
See the monta-cli package.
Example
views/base.mt
:
${ define('head'): } Monta Example ${ :end } ${ define('body') } Copyright ${ year }
views/page.mt
:
${ extends('base.html') } ${ block('head'): } Monta Page${ :end } ${ body('body'): } Welcome to my site, ${ name | upper() }${ :end }
app.js
:
const Monta = ; const monta = templateRoot: './views' ; { const result = await monta; console;};
Output:
Monta Page Welcome to my site, WOUBUC Copyright 2019
Why's it called Monta?
Uninspired as I was, I used this to find a name for the project, and Monta was the first name that I didn't hate and that wasn't taken on npm.
There is no further meaning to the name.