Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/kosatyi/ejs
- Owner: kosatyi
- License: mit
- Created: 2022-05-11T08:27:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-19T10:58:28.000Z (10 months ago)
- Last Synced: 2024-05-21T05:20:38.768Z (7 months ago)
- Topics: ejs, ejs-express, ejs-template-engine, ejs-templates
- Language: JavaScript
- Homepage:
- Size: 718 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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%><% }) %>
<% }) %>
```