Coder Social home page Coder Social logo

jonschlinkert / engine Goto Github PK

View Code? Open in Web Editor NEW
18.0 4.0 5.0 52 KB

Alternative to Lo-Dash template with support for helpers.

Home Page: https://github.com/jonschlinkert

License: Other

JavaScript 100.00%
lodash underscore template engine render compile template-engine lodash-templates helpers ejs string interpolate delimiter regexp jonschlinkert node nodejs javascript

engine's Introduction

engine NPM version NPM downloads Build Status

Template engine based on Lo-Dash template, but adds features like the ability to register helpers and more easily set data to be used as context in templates.

Install

Install with npm:

$ npm install --save engine

Usage

var Engine = require('engine');
var engine = new Engine();

engine.helper('upper', function(str) {
  return str.toUpperCase();
});

engine.render('<%= upper(name) %>', {name: 'Brian'});
//=> 'BRIAN'

API

Create an instance of Engine with the given options.

Params

  • options {Object}

Example

var Engine = require('engine');
var engine = new Engine();

// or
var engine = require('engine')();

Register a template helper.

Params

  • prop {String}
  • fn {Function}
  • returns {Object}: Instance of Engine for chaining

Example

engine.helper('upper', function(str) {
  return str.toUpperCase();
});

engine.render('<%= upper(user) %>', {user: 'doowb'});
//=> 'DOOWB'

Register an object of template helpers.

Params

  • helpers {Object|Array}: Object or array of helper objects.
  • returns {Object}: Instance of Engine for chaining

Example

engine.helpers({
 upper: function(str) {
   return str.toUpperCase();
 },
 lower: function(str) {
   return str.toLowerCase();
 }
});

// Or, just require in `template-helpers`
engine.helpers(require('template-helpers')._);

Add data to be passed to templates as context.

Params

  • key {String|Object}: Property key, or an object
  • value {any}: If key is a string, this can be any typeof value
  • returns {Object}: Engine instance, for chaining

Example

engine.data({first: 'Brian'});
engine.render('<%= last %>, <%= first %>', {last: 'Woodward'});
//=> 'Woodward, Brian'

Creates a compiled template function that can interpolate data properties in "interpolate" delimiters, HTML-escape interpolated data properties in "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data properties may be accessed as free variables in the template. If a setting object is provided it takes precedence over engine.settings values.

Params

  • str {string}: The template string.
  • opts {Object}: The options object.
  • escape {RegExp}: The HTML "escape" delimiter.
  • evaluate {RegExp}: The "evaluate" delimiter.
  • imports {Object}: An object to import into the template as free variables.
  • interpolate {RegExp}: The "interpolate" delimiter.
  • sourceURL {string}: The sourceURL of the template's compiled source.
  • variable {string}: The data object variable name.
  • returns {Function}: Returns the compiled template function.

Example

var fn = engine.compile('Hello, <%= user %>!');
//=> [function]

fn({user: 'doowb'});
//=> 'Hello, doowb!'

fn({user: 'halle'});
//=> 'Hello, halle!'

Renders templates with the given data and returns a string.

Params

  • str {String}
  • data {Object}
  • returns {String}

Example

engine.render('<%= user %>', {user: 'doowb'});
//=> 'doowb'

About

Related projects

  • assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage
  • template-helpers: Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… more | homepage
  • template: Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… more | homepage
  • verb: Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on July 19, 2016.

engine's People

Contributors

doowb avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

engine's Issues

Async helpers support

Will be good to support it out of the box.

With doowb/async-helpers#19 it will be nothing more than just calling its .set. If the helper is regular async or callback async function it will be handled/wrapped by the async-helpers, otherwise it will be just set in the this.helpers. With .get() you will get both the wrapped and non-wrapped ones.

How to escape the expressions ${myVar} in a engine template ?

I use Generate to create my own generators and engine as template engine.

When in a template I have a line including a template string (${myVar}), engine seems trying to find the myVar value to inject it in the template.

But here I'm generating a file containing some template strings like in es6 template strings of bash template string.

See below:

As you can see, here the template for the generating file .gitlab-ci.yml include the following line:

name: ${CI_COMMIT_REF_NAME}${CI_JOB_NAME}

But I need this line to be unchanged after the template have been rendered.

  1. How to escape the expressions ${myVar} in a engine template ?
  2. Is the expressions ${myVar} and <%= myVar %> equivalent ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.