Skip to main content
The Deno 2 Release Candidate is here
Learn more

NHttp View

View template engine for Deno nhttp based on nunjucks by default.

Nunjucks is a full featured templating engine for javascript. It is heavily inspired by jinja2. View the docs here.

Thanks to nunjucks-deno ported from Nunjucks.

Installation

deno.land

import { ViewEngine } from "https://deno.land/x/nhttp_view@0.1.1/mod.ts";

nest.land

import { ViewEngine } from "https://x.nest.land/nhttp_view@0.1.1/mod.ts";

Usage

// server.ts
import { NHttp, RequestEvent } from "https://deno.land/x/nhttp@0.2.5/mod.ts";
import { ViewEngine } from "https://deno.land/x/nhttp_view@0.1.1/mod.ts";

const app = new NHttp<RequestEvent & ViewEngine>();

app.use(ViewEngine.init());

app.get("/hello", ({ response }) => {
    return response.view('index', {
        name: "John",
        title: "Page Title"
    });
});

app.listen(3000);
<!-- views/index.html -->
<html>
<head>
  <title>{{ title }}</title>
</head>
<body>
  <h1>hello, {{ name }}</h1>
</body>
</html>

Run

deno run --allow-net --allow-read --unstable yourfile.ts

Default Config

...
app.use(ViewEngine.init({
    basedir: "views",
    autoescape: true,
    throwOnUndefined: false,
    trimBlocks: false,
    lstripBlocks: false,
    noCache: false,
    web: {
        useCache: false,
        async: false
    }
}));
...

Custom

import * as dejs from "https://deno.land/x/dejs@0.9.3/mod.ts";
import { NHttp, RequestEvent } from "https://deno.land/x/nhttp@0.2.5/mod.ts";
import { ViewEngine } from "https://deno.land/x/nhttp_view@0.1.1/mod.ts";

const app = new NHttp<RequestEvent & ViewEngine>();

app.use(ViewEngine.custom(dejs.renderFileToString, {
    basedir: `${Deno.cwd()}/views/`,
    extname: '.ejs'
}));

app.get("/hello", ({ response }) => {
    return response.view('index', {
        name: "John",
        title: "Page Title"
    });
});

app.listen(3000);

License

MIT