Code Monkey home page Code Monkey logo

bem-xjst's Introduction

BEM-XJST

Declarative template engine for the browser and server.

NPM version Build Status Dependency Status devDependency Status Coverage Status

Online demo. Twitter account: @bemxjst

Installation

Install it by npm: npm install bem-xjst.

Usage

As a node.js module

var bemxjst = require('bem-xjst');
var bemhtml = bemxjst.bemhtml;

// Add templates
var templates = bemhtml.compile(function() {
  block('b').content()('yay');
});

// Apply templates to data context in BEMJSON format and get result as HTML string
templates.apply({ block: 'b' });
// Result: <div class="b">yay</div>
var bemxjst = require('bem-xjst');
var bemtree = bemxjst.bemtree;

// Add templates
var templates = bemtree.compile(function() {
  block('b').content()('yay');
});

// Apply templates to data context in BEMJSON format and get result as BEMJSON
templates.apply({ block: 'b' });
// Result: { block: 'b1', content: 'yay' }

As a CLI tool

CLI can be used for creation bundles. See Compiler generate.

$ bem-xjst --help

Usage:
  bem-xjst [OPTIONS] [ARGS]


Options:
  -h, --help : Help
  -v, --version : Version
  -e, --engine : Engine name (default: bemhtml, supported: bemhtml | bemtree)
  -i INPUT, --input=INPUT : File with user templates (default: stdin)
  -o OUTPUT, --output=OUTPUT : Output bundle file (default: stdout)

API

Compiler

.compile(string or function)

Compile input templates and return templates object. (See documentation below for its methods)

.generate(string or function)

Generate output JavaScript code that might be transferred and executed in browser to get the templates object.

templates

.apply(context)

Run compiled templates on specified input context. Return resulting HTML output.

.compile(string or function)

Add more BEM templates to the templates instance. Might be called in runtime to deliver more blocks declarations to the client.

var bemxjst = require('bem-xjst');
var templates = bemxjst.bemhtml.compile(function() {
    block('b').tag()('a');
  });

templates.apply({ block: 'b' });
// Return '<a class="b"></a>'

templates.compile(function() {
  block('b').content()('Hi, folks!');
});

templates.apply({ block: 'b' });
// Return '<a class="b">Hi, folks!</a>'

.BEMContext

Constructor of the this object available in template bodies. Might be amended to expose some functionality to the templates, or to add _flush method.

var bemxjst = require('bem-xjst');
var templates = bemxjst.bemhtml.compile('');

templates.BEMContext.prototype.myField = 'opa';

templates.compile(function() {
  block('b').content()(function() {
    return this.myField;
  });
});

templates.apply({ block: 'b' });
// Return '<div class="b">opa</div>'

Benchmarks

To run benchmarks:

cd bench/
npm install
node run.js -h
node run.js

Benchmarks could be run in --compare mode to verify absence of regression in comparison to previous bem-xjst version. Make sure that the benchmarks/package.json has the right git hash of bem-xjst before running!

Documentation

License

Code and documentation copyright 2016 YANDEX LLC. Code released under the Mozilla Public License 2.0.

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.