Giter Site home page Giter Site logo

dejs's Introduction

dejs

Build Status

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";

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>`;

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";

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

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.