Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexkander/basic-ejs-layout
Template system based on EJS for nodejs/express/loopback 3.x modules
https://github.com/alexkander/basic-ejs-layout
Last synced: about 1 month ago
JSON representation
Template system based on EJS for nodejs/express/loopback 3.x modules
- Host: GitHub
- URL: https://github.com/alexkander/basic-ejs-layout
- Owner: alexkander
- License: mit
- Created: 2018-09-21T19:57:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T21:54:06.000Z (almost 2 years ago)
- Last Synced: 2024-10-03T09:29:35.583Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 325 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
basic-ejs-layout
===============[![npm version](https://badge.fury.io/js/basic-ejs-layout.svg)](https://badge.fury.io/js/basic-ejs-layout) [![Build Status](https://travis-ci.org/arondn2/basic-ejs-layout.svg?branch=master)](https://travis-ci.org/arondn2/basic-ejs-layout)
[![Coverage Status](https://coveralls.io/repos/github/arondn2/basic-ejs-layout/badge.svg?branch=master)](https://coveralls.io/github/arondn2/basic-ejs-layout?branch=master)Template system based on EJS for express/loopback 3.x modules
## Installation
`npm install basic-ejs-layout --save`
## Usage
#### Include
```js
var Layout = require('basic-ejs-layout');
```#### `new Layout(globalsLocals = {}, transform = null)`;
Create a instance to render views. The views will be render with at least
`globalsLocals` vars. The `transform` params allow customize how vars will be available
in the view.##### Arguments
Name | Type | Description
-----------------|------------|-------------
`globalsLocals` | `object` | Vars to all views.
`transform` | `function` | Function to custom how vars will be available in the view.##### Example
```js
// Option 1
var layout = new Layout();// Option 2
var layout = new Layout({
var1: "one",
var2: "two",
});// Option 3
var layout = new Layout(function (locals, layout) {
return {
ly: layout,
vars: locals
};
}));// Option 4
var layout = new Layout({
var1: "one",
var2: "two",
}, function (locals, layout) {
return {
ly: layout,
vars: locals
};
});
```#### `layout.render(filePath, locals = {}, ejsOpts = {})`
Render a the file indicated in `filePath`, with vars in `locast`, and EJS options
`ejsOpts`. Vars in `locals` are extend from `globalsLocals` in `new Layout` call.
Return the view rendered.##### Arguments
Name | Type | Description
------------|-----------|-------------
`filePath` | `string` | View path to render.
`locals` | `object` | Vars to include in rendering.
`ejsOpts` | `object` | Options to EJS render.##### Example
```js
var layout = new Layout('./myviews', { appname: 'My App' });
var rendered = layout.render('myview.ejs', { message: 'Hello world' });
// Render ./myviews/myview.ejs file with vars appname='My app' and message='Hello world'.```
#### `layout.parent(filePath)`
Indicate a view is render into another view. Child view is set in property
`body` from renderer instance. This require send layout instance into vars to
render.##### Arguments
Name | Type | Description
------------|-----------|-------------
`filePath` | `string` | Directory where find views.##### Example
```js
var layout = new Layout('./myviews');
var rendered = layout.render('myview.ejs', { ly: layout }); // Render ./myviews/myview.ejs
```./myviews/myview.ejs
```html
<% ly.parent('myparent.ejs') %>Luke: Nooooo!!!
```./myviews/myparent.ejs
```htmlLuke: He told me you kill him
Darth Vader: No, I am your father
<%- ly.body %>
```Result:
```htmlLuke: He told me you kill him
Darth Vader: No, I am your father
Luke: Nooooo!!!
```#### `layout.include(filePath, locals = {})`
Render a view into another view. Vars in `locals` will be add into rendering
view. Return the view rendered.##### Arguments
Name | Type | Description
------------|-----------|-------------
`filePath` | `string` | Directory where find views.
`locals` | `object` | Vars to include in rendering.##### Example
```js
var layout = new Layout('./myviews');
var rendered = layout.render('myview.ejs', { ly: layout }); // Render ./myviews/myview.ejs
```./myviews/myview.ejs
```htmlLuke: He told me you kill him
Darth Vader: No, I am your father
<%- ly.include('otherview.ejs', { destinatary: 'Luke' }) %>
```./myviews/otherview.ejs
```html<%- destinatary %>: Nooooo!!!
```Result:
```htmlLuke: He told me you kill him
Darth Vader: No, I am your father
Luke: Nooooo!!!
```#### `layout.engine(globalsLocals = {}, transform = null)`
Return a callback to set in a engine render. This method has the same contructor's params.##### Arguments
Name | Type | Description
-----------------|------------|-------------
`globalsLocals` | `object` | Vars to all views.
`transform` | `function` | Function to custom how vars will be available
in the view.##### Example
```js
var app = express();
app.set('view engine', 'ejs');
app.set('views', 'myviews'); // Optional.// Option 1
app.engine('ejs', Layout.engine());// Option 2
app.engine('ejs', Layout.engine({
myVar: 'myValue'
}));// Option 3
app.engine('ejs', Layout.engine(function (locals, layout) {
return {
ly: layout,
vars: locals
};
}));// Option 4
app.engine('ejs', Layout.engine({
myVar: 'myValue'
}, function (locals, layout) {
return {
ly: layout,
vars: locals
};
}));app.get('/', function (req, res) {
// myview.ejs in views folder set in express app.
res.render('myview', {
displayName: 'Alexander'
});
});
``````html
<%= displayName %> // 'Alexander'
<%= myVar %> // Generate an error<%= displayName %> // 'Alexander'
<%= myVar %> // 'myValue'<% ly.parent('mytemplate') %>
<%= vars.displayName %> // 'Alexander'
<%= vars.myVar %> // undefined<% ly.parent('mytemplate') %>
<%= vars.displayName %> // 'Alexander'
<%= vars.myVar %> // 'myValue'```
## Troubles
If you have any kind of trouble with it, just let me now by raising an issue on
the GitHub issue tracker here:https://github.com/arondn2/basic-ejs-layout/issues
Also, you can report the orthographic errors in the READMEs files or comments. Sorry for that, English is not my main language.
## Tests
`npm test` or `npm run cover`