Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/kosatyi/ejs

EJS templates with extend/block support
https://github.com/kosatyi/ejs

ejs ejs-express ejs-template-engine ejs-templates

Last synced: about 1 month ago
JSON representation

EJS templates with extend/block support

Awesome Lists containing this project

README

        

# EJS

Embedded JavaScript templates with extend/block

[![npm](https://img.shields.io/npm/v/@kosatyi/ejs.svg)](https://www.npmjs.com/package/@kosatyi/ejs)
[![github-issues](https://img.shields.io/github/issues/kosatyi/ejs.svg)](https://github.com/kosatyi/ejs/issues)
[![license](https://img.shields.io/npm/l/@kosatyi/ejs.svg)](https://github.com/kosatyi/ejs/blob/master/LICENCE)

## Install

You can get EJS via [npm](http://npmjs.com).

```bash
$ npm install @kosatyi/ejs --save
```

## Usage

```js
const ejs = require('@kosatyi/ejs');

// path where templates is located
ejs.configure({
path: 'views'
})

// add custom template helper functions
ejs.helpers({
ucase(text){
return String(text).toUpperCase()
}
})

// load index.ejs template from `views` folder
ejs.render('page/index',{
posts:[{
title:'Post Title',
content:"

post content

"
}]
}).then((content)=>{
console.log(content)
})
```

## Use with Express

```bash
$ npm install @kosatyi/ejs --save
```

```js
const express = require('express')
const ejs = require('@kosatyi/ejs')
const app = express()
app.engine('ejs', ejs.__express)
app.set('views', 'views')
app.set('view cache', false)
app.set('view engine', 'ejs')
```

or use `ejs` alias

```bash
$ npm install ejs@npm:@kosatyi/ejs --save
```

```js
const express = require('express')
const app = express()
app.set('views', 'views')
app.set('view engine', 'ejs')
app.set('view cache', false)
```

## Template Example

**layout/default.ejs**

```ejs


<%-ejs.get('title')%>
<% ejs.block('resources',()=>{ %>

<% }) %>



<% ejs.block('header',()=>{ %>

<%-ejs.get('title')%>


<% }) %>


<% ejs.block('content') %>


<% ejs.block('footer',()=>{ %>
Copyright
<% }) %>

```

**page/index.ejs**

```ejs
<% ejs.extend('layout/default') %>

<% ejs.set('title','Page Title') %>

<% ejs.block('resources',(parent)=>{ %>
<% parent() %>

<% }) %>

<% ejs.block('content',()=>{ %>

<% ejs.each('posts',(post)=>{ %>

<%-post.title%>


<%=post.content%>

<% }) %>

<% }) %>
```