bem / bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
https://bem.github.io/bem-xjst
Other
116 stars 48 forks source link

Using bemhtml as a commonjs module #156

Closed mightyaleksey closed 8 years ago

mightyaleksey commented 8 years ago

Hi,

I'd like to make a webpack-loader for bemhtml and I lack possibility to use bemhtml as a commonjs module. Something similar to bem/bh:

const BH = require('bh').BH;
const bh = new BH();

// describe templates
bh.match('template name', ctx => {});
bh.match('template name2', ctx => {});

// and etc..

// launch
bh.apply({ block: 'button' }); // '<button class="button"></button>'

Is it possible?

Kind regards, Alexey

tadatuta commented 8 years ago

sure:

npm i bem-xjst
echo "" | bem-xjst -o bundle.bemhtml.js
const BEMHTML = require('./bundle.bemhtml.js');

BEMHTML.compile(function() {
    block('b1').tag()('ololo');
    block('b2').attrs()({ a: 'b' });
});

BEMHTML.apply({ block: 'b1', content: { block: 'b2' } }) // <ololo class="b1"><div class="b2" a="b"></div></ololo>
mightyaleksey commented 8 years ago

Surprisingly it works :) Will be it possible to skip the bundling step in future?

Thanks

tadatuta commented 8 years ago

Will be it possible to skip the bundling step in future?

we will investigate the way bundling works now as it makes gathering coverage somewhat hard. if we'll find a way how to avoid it, we'll definitely let users know.