Socket
Socket
Sign inDemoInstall

gulp-ejs-template

Package Overview
Dependencies
56
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gulp-ejs-template

Precompile EJS templates to a JS file.


Version published
Weekly downloads
9
Maintainers
1
Install size
783 kB
Created
Weekly downloads
 

Readme

Source

gulp-ejs-template

Precompile EJS templates to a JS file.

Install

Install with npm

npm install --save-dev gulp-ejs-template

Usage

var gulpEjs = require('gulp-ejs-template');

gulp.task('ejsTemplate', function () {
  return gulp.src('test/fixtures/*.html')
  .pipe(gulpEjs({
    moduleName: 'templates'
  }))
  .pipe(gulp.dest('test'));
});

Demo

test/fixtures/header.html:

<p><%= it.title || 'gulp' %> module</p>
<%- include('user.html', it.user) %>

test/fixtures/user-list.html:

<ul>
  <% users.forEach(function(user) { -%>
    <li>
      <%= user.name %>
    </li>
  <% }) -%>
</ul>

test/fixtures/user.html:

<h1><%= it.name %></h1>

precompile to test/templates.js:

;(function(root, factory) {
  'use strict';

  if (typeof module === 'object' && module.exports) module.exports = factory();
  else if (typeof define === 'function' && define.amd) define([], factory);
  else root.templates = factory();
}(typeof window === 'object' ? window : this, function() {
  'use strict';
  var templates = {};

  templates['header']  = templates['header.html'] = function(it) {
    var locals = it, __output = "";
    var include = function(tplName, data) { return render(tplName, data); }
    ;__output += "<p>";;__output += escape(it.title || 'gulp');__output += " module</p>\n";;__output = [__output, include('user.html', it.user)].join("");__output += "\n";
    return __output.trim();
  };

  templates['user-list']  = templates['user-list.html'] = function(it) {
    var locals = it, __output = "";
    ;__output += "<ul>\n  ";; users.forEach(function(user) { ;__output += "    <li>\n      ";;__output += escape(user.name);__output += "\n    </li>\n  ";; }) ;__output += "</ul>\n";
    return __output.trim();
  };

  templates['user']  = templates['user.html'] = function(it) {
    var locals = it, __output = "";
    ;__output += "<h1>";;__output += escape(it.name);__output += "</h1>\n";
    return __output.trim();
  };

  var ejs = {
    locals: {},
    get: getTpl,
    render: render
  };
  return ejs;

  function render(tplName, data) {
    var it  = copy({}, ejs.locals);
    return getTpl(tplName)(copy(it, data));
  }

  function getTpl(tplName) {
    return templates[tplName];
  }

  function escape(markup) {
    if (!markup) return '';
    return String(markup)
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/'/g, '&#39;')
      .replace(/"/g, '&quot;');
  }

  function copy(to, from) {
    from = from || {};
    for (var key in from) to[key] = from[key];
    return to;
  }
}));

API

var gulpEjs = require('gulp-ejs-template');

gulp.task('ejsTemplate', function () {
  return gulp.src('test/fixtures/*.html')
    .pipe(gulpEjs({/*options*/}))
    .pipe(gulp.dest('test'));
});

options.moduleName

Optional, Type: String, Default: 'templates'.

Name of the templates module.

options.delimiter

Optional, Type: String, Default: %.

ejs's delimiter.

templates funciton

var ejs = require('templates.js');

ejs.locals = {}

ejs.locals = {
  local: 'en',
  __: function(i18nStr) {/* i18n function */}
}

ejs.get(tplName)

var tplFunction = ejs.get('header');

ejs.render(tplName, data)

var tpl = ejs.render('header', {
  title: 'gulp-ejs-template',
  user: {
    name: 'zensh'
  }
});

License

MIT © Teambition

Keywords

FAQs

Last updated on 28 Jan 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc