basic-ejs-layout
Template system based on EJS for express/loopback 3.x modules
Installation
npm install basic-ejs-layout --save
Usage
Include
var 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
// Option 1var layout = ; // Option 2var layout = var1: "one" var2: "two"; // Option 3var layout = { return ly: layout vars: locals ;}); // Option 4var layout = var1: "one" var2: "two" { 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
var layout = './myviews' appname: 'My App' ;var rendered = layout;// 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
var layout = './myviews';var rendered = layout; // Render ./myviews/myview.ejs
./myviews/myview.ejs
<% ly.parent('myparent.ejs') %>Luke: Nooooo!!!
./myviews/myparent.ejs
Luke: He told me you kill himDarth Vader: No, I am your father<%- ly.body %>
Result:
Luke: He told me you kill himDarth Vader: No, I am your fatherLuke: 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
var layout = './myviews';var rendered = layout; // Render ./myviews/myview.ejs
./myviews/myview.ejs
Luke: He told me you kill himDarth Vader: No, I am your father<%- ly.include('otherview.ejs', { destinatary: 'Luke' }) %>
./myviews/otherview.ejs
<%- destinatary %>: Nooooo!!!
Result:
Luke: He told me you kill himDarth Vader: No, I am your fatherLuke: 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
var app = ;app;app; // Optional. // Option 1app; // Option 2app; // Option 3app; // Option 4app; app;
<!-- myviews/myview.ejs --> <!-- Option 1 --> <%= displayName %> // 'Alexander' <%= myVar %> // Generate an error <!-- Option 2 --> <%= displayName %> // 'Alexander' <%= myVar %> // 'myValue' <!-- Option 3 --> <% ly.parent('mytemplate') %> <%= vars.displayName %> // 'Alexander' <%= vars.myVar %> // undefined <!-- Option 4 --> <% 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