express-bem
BEM bundles render adapter for express 🌴
Why
Because laziness and short memory. And because simple solutions rocks.
And now it's just a npm i express-bem
and 3 lines of code to use bem blocks library in any app.
Plugins
- express-bem-bemtree
- express-bem-bemhtml
- express-bem-bh
- express-bem-tools-make bem-tools make middleware
- express-bem-enb-make enb make middleware
Dependencies
Peer
express v3.0+
TL;DR
Quick start guide. Run it in your shell:
git clone https://github.com/express-bem/project-stub.git ./express-bem-project-stubcd express-bem-project-stubnpm installPORT=3030 node app.js
And after start:
open http://localhost:3030/
Look closer at stub: https://github.com/express-bem/project-stub
Installation
$ npm i express-bem --save
Usage
To load and init module you can use this snippet:
var Express = ;var ExpressBem = ; // create app and bemvar app = ;var bem = ; // here to lookup bundles at your path you need small patchappbem = bem; // register enginesbem; // requires module express-bem-bemtreebem; // ... express-bem-bemhtml
Allowed options for cache
param object are:
load
if set tofalse
will reload any template files each timeexec
if set tofalse
will exec template files each time
But also can be set to boolean.
Examples:
var bem = ; var cachingbem = ;
Also you can add your custom engine
bem;
Or even more complex (call this before loading plugins or set default engine by self):
bem;
See also ExpressBem.prototype.bindTo
method.
And then just use res.render
(or app.render
) in your code and pass
some data (or bemjson
tree) there:
app
Or raw data to execute bemtree
app;
Directories
If you want to use your bem
repo you can just pull it with git
submodules
or install as npm
package and then just use it as your special bundles path.
.
├── app.js
└── node_modules
└── my-super-mockup
├── desktop.blocks
│ └──...
└── desktop.bundles
├── index
│ ├── index.bemhtml.js
│ └── index.bemtree.js
└── layout
├── layout.bemhtml.js
└── layout.bemtree.js
Otherwise you can import somehow bemhtml.js
and/or bemtree.js
files
to your views
path and use them with default View.prototype.lookup
.
.
├── app.js
└── views
├── index.bemhtml.js
├── index.bemtree.js
├── layout.bemhtml.js
└── layout.bemtree.js
Plugins, engines, middlewares
Engine
Very simple async render engine
/** * At least one name/ext definition required in params or engine * @method engine * @param * @param * @param */ // canonicalbem; // name will be esplbem; // ext will be .espl.jsbem; // via function { ;}evenSimplerextension = '.espl.js';bem; // via blackbox (function/object)bem; // via objectbem;
You should know that you should set by self default engine if you don't use .bindTo
method. Default engine is the first declared engine.
Like that:
// set first available engine as defaultapp; // set concrete default engineapp;
Middleware
Middlewares usually calls betweed express' View.prototype.render
and engine's .render
.
/** * @method use * @param * @param */ // using as simple middlewarebem; // using as generatorbem
Plugin interface
It should have engines
(engine
if one) and/or middlewares
(middleware
) properties.
To load plugin into express-bem
instance just call usePlugin
method with some parameters:
/** * @method usePlugin * @param * @param */ // by module requirebem; // by object declarationbem; // by function generatorbem;
License
MIT. See also License