# ejt

> Compiler for JavaScript-based HTML templating language

Latest version **0.3.0** (published 2016-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install ejt
pnpm add ejt
yarn add ejt
bun add ejt
```

Provides the command `ejt`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2016-01-21 |
| First published | 2016-01-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.4.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | eliot-akira |
| Maintainers | eliot |
| Keywords | template, html, javascript |

## Links

- npm: https://www.npmjs.com/package/ejt
- Repository: https://github.com/eliot-akira/ejt
- Homepage: https://github.com/eliot-akira/ejt#readme
- Issues: https://github.com/eliot-akira/ejt/issues
- npm.io page: https://npm.io/package/ejt

## Dependencies (3)

- [marked](https://npm.io/package/marked.md) ^0.3.5
- [optimist](https://npm.io/package/optimist.md) ^0.6.1
- [through2](https://npm.io/package/through2.md) ^2.0.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2016-01-21
- 0.1.2 — 2016-01-20
- 0.1.1 — 2016-01-19
- 0.1.0 — 2016-01-19
- 0.0.6 — 2016-01-17
- 0.0.3 — 2016-01-17

## README

## EJT

This is a server-side compiler for simple JavaScript-based HTML templating.

## Commands

All commands are placed between opening `<%` and closing `%>` tags.

#### Include

When the first word after the opening tag is *include*, the specified file is included into the compiled output. The path is relative to the template's current folder. The default file extension is `html` and can be omitted.

Include *partial.html* inside another HTML file

```html
<% include partial %>
```

Markdown files are rendered and included.

```html
<% include welcome.md %>
```

JSON files are parsed as an object.

```js
<% const pages = include('site.json') %>
```

Here's a twist. If the first word is not one of the reserved commands, everything between the tags is evaluated as JavaScript. In that case, `include` is a function that returns the file content.

#### Extend

*index.html*

```html
<% extend layout %>

<% block header %>
  ..Header content..
<% end %>

..Main content..
```

*layout.html*

```html
<header>
  <% content header %>
</header>
<main>
  <% content %>
</main>
```

Compiled result

```html
<header>
  ..Header content..
</header>
<main>
  ..Main content..
</main>
```

#### Variables

Escaped: `<%= foo %>`

Unescaped: `<%- bar %>`

#### Code

Escape code block and wrap in `<pre><code>`

```html
<% code %>
<h1>Code example</h1>
<% end %>
```

Optionally add attributes: `<% code class="language-markup" %>`

## Inline JS

The compiler runs on Node.js, so there are [some ES6 features](https://nodejs.org/en/docs/es6/) supported.

```js
<% const pages = include('site.json') %>

<ul>
  <% for (let path in pages) { %>
    <li>
      <a href="/<%- path %>"><%- pages[path] %></a>
    </li>
  <% } %>
</ul>
```

#### Output

The variable `Output` is a buffer holding the compiled HTML up to that point. It can be used to add content.

```js
['item1', 'item2'].forEach((product) => {
  Output += include(`products/${product}`)
})
```

#### Local

The variable `Local` is passed to every template. It has one default property `Local.file`, which is the current template path. `Local` can be used to pass data and functions you want available in other templates.


## Use


Command line

```bash
$ ejt source [destination]
```

Compiler (server-side only)

```js
var EJT = require('ejt')
var renderer = new EJT( options )

var result = renderer.compile( src )
```

Gulp

```js
var ejt = require('ejt/gulp')

gulp.task('html', function() {
  return gulp.src( srcPath )
    .pipe( ejt() )
    .pipe(gulp.dest( destPath ))
})
```

TODO: Express

```js
var ejt = require('ejt/express');

app.use( ejt );
```

## Credit

This is based on a fork of [ECT](https://github.com/baryshev/ect), with on-going refactoring and changes:

- Plain JavaScript
- Default file extension; relative path; filename without quotes
- Include markdown, etc. via extensions
- New command: code

I believe the original concept came from [John Resig's micro-templating](http://ejohn.org/blog/javascript-micro-templating/).

---
_Source: https://npm.io/package/ejt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
