Denjucks is a powerful templating language and engine for Deno applications, and provides an easy syntax for making scalable web applications using Deno.
Internally, Denjucks is a fork of Mozilla's Nunjucks for NodeJS, with the following changes:
- Uses ESM syntax instead of CommonJS syntax
- Uses Deno file system APIs instead of NodeJS file system APIs
- Wraps all NodeJS dependencies into ESM compatible syntax
To install, simply import the raw module from the github repository in your Deno code:
// @deno-types="https://deno.land/x/denjucks/mod.d.ts"
import denjucks from "https://deno.land/x/denjucks/mod.js";
When running the file the first time, the source code for denjucks will be downloaded and cached on your machine.
Rendering a template from a string:
// @deno-types="https://deno.land/x/denjucks/mod.d.ts"
import denjucks from "https://deno.land/x/denjucks/mod.js";
console.log(
denjucks.renderString("hello {{ txt }}", {txt: "world"})
);
will print to the console:
hello world
Rendering a template from a file:
Remember to include the --allow-read flag when running the deno command or an error will be thrown, as the deno sandbox prevents reads by default
With the file _base.html as the parent template:
<html>
<head></head>
<body>
{% block content %}{% endblock content %}
</body>
</html>
And the file index.html as the template that will extend the parent:
{% extends "_base.html" %}
{% block content %}
<p>Hello {{ txt }}</p>
{% endblock content %}
Calling render on index.html:
// @deno-types="https://deno.land/x/denjucks/mod.d.ts"
import denjucks from "https://deno.land/x/denjucks/mod.js";
console.log(
denjucks.render("index.html", {txt: "World"})
);
Will print to the console:
<html>
<head></head>
<body>
<p>Hello World</p>
</body>
</html>
Since Denjucks is a port of Nunjucks, it uses the exact same syntax as Nunjucks. See here for full documentation on Nunjucks.
Nunjucks is a full featured templating engine for javascript. It is heavily inspired by jinja2. View the docs here.