README
gulp-ejs-monster
:us: English | :ru: Русский язык
Gulp plugin for ejs with steroids. The project is inspired by ejs-locals
Table of contents
- Thanks
- Why this plugin was created?
- Example of using the plugin
- gulpEjsMonster()
- locals API
- Project Info
Thanks
First of all, we want to express our gratitude to the people who led us to use the template engine ejs
and create on its basis gulp-ejs-monster
:
- Matthew Eernisse (mde) - for creating and supporting
ejs
(http://ejs.co), and the community ofejs
, which helps him in this - Tom Carden (RandomEtc) - for creating a project
ejs-locals
, from which we took the idea of realizationgulp-ejs-monster
- Ryan Zimmerman (RyanZim) -
EJS-Lint
- Ariya Hidayat (ariya) -
jquery/esprima
- Corey Hart (codenothing) -
jsonlint
Why this plugin was created?
ejs
(http://ejs.co) - is a universal template engine that allows you to create any markup of any complexity. The better your knowledge of JavaScript - the more opportunities you have with ejs
.
There are already many other plugins for ejs
. But we also decided to create own, as an add-on to the ejs
+ pumping it with a small set of "steroids" ))).
Also, the main focus of the plugin gulp-ejs-monster
- was made on optimization and rendering speed.
By default, ejs
uses the JavaScript construction with (expression)
to add scope - this gives its advantages for working with the template engine, but has its own price - the speed of searching for variables increases - which affects the rendering speed of the pages. This is especially noticeable on large projects.
Therefore gulp-ejs-monster
forcibly turns off the native ejs
parameters in order to work in strict mode. That gives a significant gain to the rendering speed.
List of constant values from gulp-ejs-monster
for ejs
:
{
"strict": true,
"_with": false,
"debug": false,
"rmWhitespace": false,
"client": false
}
This approach also has its price - now only one global object is available for you, without any "proxy" properties (which the with
design used to imitate).
If this approach to working with template engine
ejs
does not suit you, you may no read further and do not create PRs, since we do not intend to change it)))
Example of using the plugin
Installation
npm i --save-dev gulp-ejs-monster
# or yarn cli
yarn add --dev gulp-ejs-monster
Gulp task
const gulp = require('gulp');
const gulpEjsMonster = require('gulp-ejs-monster');
gulp.task('ejs', function() {
return gulp.src('./src/ejs/*.ejs')
.pipe(gulpEjsMonster({/* plugin options */}))
.pipe(gulp.dest('./dist/'));
});
EJS markup
Example of a project structure
ejs/
layouts/
base.ejs
widgets/
news-list.ejs
includes/
critical.css
requires/
news-list.json
index.ejs
news.ejs
Layouts
layouts/base.ejs
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><%- locals.blocks.title %></title>
<style><%- locals.include('includes/critical.css') %></style>
</head>
<body>
<%- locals.blocks.header %>
<%- locals.body %>
</body>
</html>
Render views
index.ejs
<% locals.setLayout('layouts/base.ejs') -%>
<% locals.block('title', 'Index view') -%>
<h1>Index view</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<hr>
news.ejs
<% locals.setLayout('layouts/base.ejs') -%>
<% locals.block('title', 'Last News') -%>
<% let newsList = locals.require('requires/news-list.json') -%>
<h1>Last News</h1>
<%- locals.widget('widgets/news-list.ejs', {list: newsList}) %>
<hr>
Executables files
requires/news-list.json
[
{
"title": "News title 1",
"description": "Lorem ipsum dolor sit ....",
"href": "news-page.html"
}, {
"title": "News title 2",
"description": "Lorem ipsum dolor sit ....",
"href": "news-page.html"
}, {
"title": "News title 3",
"description": "Lorem ipsum dolor sit ....",
"href": "news-page.html"
}
]
Text files
includes/critical.css
html{font-family:sans-serif}
body{margin:0}
h1{color:red}
Widgets
widgets/news-list.ejs
<%
let {
list = []
} = locals.entry;
if (!list.length) {
return 'No news yet :((';
}
-%>
<ul class="news-list">
<% list.forEach(item => { -%>
<li class="news-list__item">
<div class="news-item">
<div class="news-item__title"><%- item.title %></div>
<div class="news-item__description">
<p><%- item.description %></p>
<p><a href="<%- item.href %>">Read more</a></p>
</div>
</div>
</li>
<% }); -%>
</ul>
gulpEjsMonster
Plugin properties
gulpEjsMonster.pluginName
Plugin name.
Plugin methods
gulpEjsMonster.preventCrash()
The method which, on error, calls the end
event to prevent the current process gulp
from falling out of the task.
Example of use
const gulp = require('gulp');
const gulpEjsMonster = require('gulp-ejs-monster');
gulp.task('ejs', function() {
return gulp.src('./src/ejs/*.ejs')
.pipe(gulpEjsMonster({/* plugin options */}).on('error', gulpEjsMonster.preventCrash))
.pipe(gulp.dest('./dist/'));
});
Plugin options
A little bit of advice - in order to speed up the processing and preparation of the plugin's parameters - use the created object with saving to a variable, which you can then specify when you call.
In this case, by storing references to an external object, the parameters will not be re-prepared. And also it is possible to save the received data (in the object locals
) from the previous page of the render to the next.
Example of use
const gulp = require('gulp');
const gulpEjsMonster = require('gulp-ejs-monster');
const options = {/* plugin options */}; // save as variable
gulp.task('ejs', function() {
return gulp.src('./src/ejs/*.ejs')
.pipe(gulpEjsMonster(options).on('error', gulpEjsMonster.preventCrash))
.pipe(gulp.dest('./dist/'));
});
Then you can see a list of all available options.
layouts
data type string
|
default process.cwd()
The relative path from the current working directory to the directory with layouts.
widgets
data type string
|
default process.cwd()
The relative path from the current working directory to the directory with widgets.
requires
data type string
|
default process.cwd()
Relative path from the current working directory to the directory with js/json files, which you can connect as executable files, using the CommonJS export modules.
includes
data type string
|
default process.cwd()
The relative path from the current working directory to the directory c with any text files from which you can connect the content as it is.
extname
data type string
|
default '.html'
Extend the resulting render files.
It is allowed not to specify. (dot) at the beginning of the value, example 'php' => '.php'
delimiter
data type string
|
default '%'
|
допустимые значения ['%', '&', '