Ejs Template Engine for Deno

Ejs Template Engine for Deno

ejs template engine for deno.

dejs

Features

Supported

  • <%= %> Output escaped value
  • <%- %> Output raw value
  • <%# %> Comment (nothing will be shown)
  • <% %> Evaluate (use control flow like: if, for)
  • include partial ejs template

Not supported

  • All other features of ejs

Usage

import * as dejs from 'https://deno.land/x/[email protected]/mod.ts';
  • renderFile(filePath: string, params: Params): Promise<Deno.Reader>
    • renders from file, outputs Deno.Reader
  • render(body: string, params: Params): Promise<Deno.Reader>
    • renders from string, outputs Deno.Reader
  • renderFileToString(filePath: string, params: Params): Promise<string>
    • renders from file, outputs string
  • renderToString(body: string, params: Params): Promise<string>
    • renders from string, outputs string
  • compile(reader: Reader): Promise<Template>
    • only compiles ejs and returns Template(params: Params): string
    • use this to cache compiled result of ejs

Render from file

  • template.ejs
<body>
  <% if (name) { %>
    <h1>hello, <%= name %>!</h1>
  <% } %>
</body>
  • index.ts
const { cwd, stdout, copy } = Deno;
import { renderFile } from 'https://deno.land/x/dejs/mod.ts';

(async () => {
  const output = await renderFile(`${cwd()}/template.ejs`, {
    name: 'world',
  });
  await copy(output, stdout);
})();
  • console
$ deno index.ts
<body>

    <h1>hello, world!</h1>

</body>

Render from string

const { cwd, stdout, copy } = Deno;
import { render } from 'https://deno.land/x/dejs/mod.ts';

const template = `<body>
  <% if (name) { %>
    <h1>hello, <%= name %>!</h1>
  <% } %>
</body>`;

(async () => {
  const output = await render(template, {
    name: 'world',
  });
  await copy(output, stdout);
})();

Include partial ejs template

  • To include template from other file, use include function in ejs.
  • include resolves views from relative path from executed ts / js file. (not from ejs template file).
    • This behavior may change in the future.
Usage
await include(filePath, params)
Example
  • views/header.ejs
<html>
<head>
  <title><%- title %></title>
</head>
<body>
  • views/footer.ejs
</body>
</html>
  • views/main.ejs
<%- await include('views/header.ejs', { title: 'include example' }) %>
<h1>hello, world!</h1>
<%- await include('views/footer.ejs') %>
  • index.ts
const { cwd, stdout, copy } = Deno;
import { renderFile } from 'https://deno.land/x/dejs/mod.ts';

(async () => {
  const output = await renderFile(`${cwd()}/views/main.ejs`);
  await copy(output, stdout);
})();
  • console
$ deno index.ts
<html>
<head>
  <title>include example</title>
</head>
<body>
<h1>hello, world!</h1>
</body>
</html>

Limitations

  • backslashes at line end will removed.

Development

Update modules

  • Please use dem
dem update https://deno.land/[email protected]

Lint

  • make lint

Format

  • make fmt

Testing

  • make test

Author

syumai

Download Details:

Author: syumai

Source Code: https://github.com/syumai/dejs

deno nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings