Getting Started
Install
Deno.land
With Deno, you can just import Vento from HTTP:
import vento from "https://deno.land/x/vento@v2.0.0/mod.ts";
NPM
Vento is also published on NPM:
npm install ventojs
And then import Vento:
import vento from "ventojs";
Browser
Vento code is isomorphic, meaning that it works consistently across any JavaScript environment without any compilation step. To use Vento in the browser, just download the code from NPM or import it from a CDN like jsDelivr:
import vento from "https://cdn.jsdelivr.net/npm/ventojs@2.0.0/web.js"
Usage
First, create an instance of Vento.
const env = vento();
load
Use the load
function to load and compile a template from a path. The compiled
templates are stored in an internal cache, so they are only compiled once.
const template = await env.load("my-template.vto");
const result = await template({ title: "Hello, world!" });
run
Alternatively, you can load and run the template file in a single call:
const result = await env.run("my-template.vto", { title: "Hello, world!" });
runString
If the template code is not in a file, you can run it directly:
const result = await env.runString("<h1>{{ title }}</h1>", {
title: "Hello, world!",
});
console.log(result.content);
// <h1>Hello, world!</h1>
Print errors
Vento throws a VentoError
whenever it encounters an error during template compilation or rendering. To display errors in a readable format, use the printError
helper:
import vento from "ventojs/mod.ts";
import { printError } from "ventojs/core/errors.ts";
const env = vento({
includes: "./templates",
});
try {
const result = await env.run("main.vto");
} catch (error) {
await printError(error);
}
The second argument allows to customize the format. For example, to use colors in the terminal:
await printError(error, "colors");
Clearing cache
Vento implements a cache to prevent excessive compilation. If you need to reload your templates at runtime (ie. for development), you can clear the cache.
env.cache.clear();