Giter VIP home page Giter VIP logo

nano-ejs's Introduction

Gitter NPM version Build status Test coverage Dependency Status License Downloads

nano-ejs

A very small, simple and fast EJS compiler.

Usage

> var Ejs = require('nano-ejs');
> console.log(Ejs.compile('test <?=one?>', 'one')('ola'));
test ola
> _

EJS syntax

JS expressions embedding <?=JS_EXPRESSION?>

<!DOCTYPE>
<html><head><title><?=PAGE_TITLE?></title></head><body></body></html>

JS statements embedding <? JS_STATEMENTS ?>

<!DOCTYPE>
<html><head><title><?=PAGE_TITLE?></title></head><body><?
  if (V1) {
    ?>V1<?
  } else {
    ?>V2<?
  }
?></body></html>

JS global calls embedding

The same as <?=global.function()?>, where 'global' can be redefined by EJS object option 'global_id'.

This construction useful then you pass a some object to EJS function like...

var Ejs = require('nano-ejs'),
    css_helpers = require('css-helpers'); // hypotetical module

var css_text = '\
.error   { color: <?.hsv2hrgb(0, 1, 1)?>; }\n\
.warning { color: <?.hsv2hrgb(120, 1, 1)?>; }\n\
';

var ejs_fn = Ejs.compile(css_text, 'css_helpers', { global_id: 'css_helpers' });
console.log(ejs_fn(css_helpers));

console output:

.error   { color: #FF0000; }\n\
.warning { color: #00FF00; }\n\

API

Ejs.compile(text[, args[, options]])

  • text String EJS text
  • args String compiled function arguments list (for new Function (args, body))
  • options Object
  • open_str String open embedded JS code symbols sequence (the default value is '<?')
  • close_str String clode embedded JS code symbols sequence (the default value is '?>')
  • global_id String identifier of global object (like global or window) (the default value is 'global')

class: Ejs

new Ejs(options)

  • options Object
  • open_str String open embedded JS code symbols sequence (the default value is '<?')
  • close_str String clode embedded JS code symbols sequence (the default value is '?>')
  • global_id String identifier of global object (like global or window) (the default value is 'global')

EJS parser context.

ejs.is_ejs(text)

return Boolean

Check the text for EJS injections. Returns true for EJS texts.

ejs.push_ejs(text)

  • text String EJS text

Parse EJS text and append to the final JS code.

ejs.push_js(text)

  • text String JS text

Parse JS with texts injections (?>test<?) and append to the final JS code.

ejs.compile(args)

  • args String compiled function arguments list (for ```new Function (args, body)``)

Returns compiled JS function of EJS source.

ejs.listing()

Returns JavaScript code translated from EJS.

> console.log(new (require('nano-ejs'))().push_ejs('text<?=5?>--').push_code().listing());
$.push('text', 5+'', '--');
> _

nano-ejs's People

Contributors

holixus avatar

Watchers

 avatar

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.