- <%= %> Output escaped value
- <%- %> Output raw value
- <%# %> Comment (nothing will be shown)
- <% %> Evaluate (use control flow like: if, for)
- include partial ejs template
- All other features of ejs
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
- only compiles ejs and returns
- 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";
const output = await renderFile(`${cwd()}/template.ejs`, {
name: "world",
});
await copy(output, stdout);
- console
$ deno index.ts
<body>
<h1>hello, world!</h1>
</body>
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>`;
const output = await render(template, {
name: "world",
});
await copy(output, stdout);
- 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.
await include(filePath, params)
- 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";
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>
- backslashes at line end will removed.
- Please use dem
dem update https://deno.land/[email protected]
make lint
make fmt
make test
syumai
MIT