Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/syumai/dejs

ejs template engine for deno.
https://github.com/syumai/dejs

deno ejs typescript

Last synced: 2 months ago
JSON representation

ejs template engine for deno.

Awesome Lists containing this project

README

        

# dejs

[![Build Status](https://github.com/syumai/dejs/workflows/test/badge.svg?branch=master)](https://github.com/syumai/dejs/actions)

- [ejs](https://ejs.co) template engine for
[deno](https://github.com/denoland/deno).

## 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

```ts
import * as dejs from "https://deno.land/x/[email protected]/mod.ts";
```

- **`renderFile`** `(filePath: string, params: Params): Promise`
- renders from file, outputs Deno.Reader
- **`render`** `(body: string, params: Params): Promise`
- renders from string, outputs Deno.Reader
- **`renderFileToString`** `(filePath: string, params: Params): Promise`
- renders from file, outputs string
- **`renderToString`** `(body: string, params: Params): Promise`
- renders from string, outputs string
- **`compile`** `(reader: Reader): Promise`
- only compiles ejs and returns `Template(params: Params): string`
- use this to cache compiled result of ejs

### Render from file

- template.ejs

```ejs

<% if (name) { %>

hello, <%= name %>!


<% } %>

```

- index.ts

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

```sh
$ deno index.ts

hello, world!

```

### Render from string

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

const template = `
<% if (name) { %>

hello, <%= name %>!


<% } %>
`;

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

```ejs
await include(filePath, params)
```

#### Example

- views/header.ejs

```ejs

<%- title %>

```

- views/footer.ejs

```ejs

```

- views/main.ejs

```
<%- await include('views/header.ejs', { title: 'include example' }) %>

hello, world!


<%- await include('views/footer.ejs') %>
```

- index.ts

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

```sh
$ deno index.ts

include example

hello, world!

```

## Limitations

- backslashes at line end will removed.

## Development

### Update modules

- Please use [dem](https://github.com/syumai/dem)

```
dem update https://deno.land/[email protected]
```

### Lint

- `make lint`

### Format

- `make fmt`

### Testing

- `make test`

## Author

syumai

## License

MIT