mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 06:50:47 -07:00
feature: plugin API, working on #86
This commit is contained in:
@@ -230,6 +230,25 @@ engine.registerTag('upper', {
|
|||||||
|
|
||||||
See existing tag implementations here: <https://github.com/harttle/liquidjs/blob/master/tags/>
|
See existing tag implementations here: <https://github.com/harttle/liquidjs/blob/master/tags/>
|
||||||
|
|
||||||
|
## Plugin API
|
||||||
|
|
||||||
|
A pack of tags or filters can be encapsulated into a **plugin**, which will be typically installed via npm.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
engine.plugin(require('./some-plugin'));
|
||||||
|
|
||||||
|
// some-plugin.js
|
||||||
|
module.exports = function (Liquid) {
|
||||||
|
// here `this` refers to the engine instance
|
||||||
|
// `Liquid` provides facilities to implement tags and filters
|
||||||
|
this.registerFilter('foo', x => x);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Plugin List:
|
||||||
|
|
||||||
|
* To add your plugin, contact me or simply send a PR.
|
||||||
|
|
||||||
[nunjucks]: http://mozilla.github.io/nunjucks/
|
[nunjucks]: http://mozilla.github.io/nunjucks/
|
||||||
[liquid-node]: https://github.com/sirlantis/liquid-node
|
[liquid-node]: https://github.com/sirlantis/liquid-node
|
||||||
[shopify/liquid]: https://shopify.github.io/liquid/
|
[shopify/liquid]: https://shopify.github.io/liquid/
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ const _engine = {
|
|||||||
registerTag: function (name, tag) {
|
registerTag: function (name, tag) {
|
||||||
return this.tag.register(name, tag)
|
return this.tag.register(name, tag)
|
||||||
},
|
},
|
||||||
|
plugin: function (plugin) {
|
||||||
|
return plugin.call(this, Liquid)
|
||||||
|
},
|
||||||
express: function (opts) {
|
express: function (opts) {
|
||||||
opts = opts || {}
|
opts = opts || {}
|
||||||
const self = this
|
const self = this
|
||||||
|
|||||||
@@ -12,6 +12,24 @@ describe('Liquid', function () {
|
|||||||
}).to.throw(/illegal root/)
|
}).to.throw(/illegal root/)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#plugin()', function () {
|
||||||
|
it('should call plugin on the instance', async function () {
|
||||||
|
const engine = new Liquid()
|
||||||
|
engine.plugin(function (Liquid) {
|
||||||
|
this.registerFilter('foo', x => `foo${x}foo`)
|
||||||
|
})
|
||||||
|
const html = await engine.parseAndRender('{{"bar"|foo}}')
|
||||||
|
expect(html).to.equal('foobarfoo')
|
||||||
|
})
|
||||||
|
it('should call plugin with Liquid', async function () {
|
||||||
|
const engine = new Liquid()
|
||||||
|
engine.plugin(function (Liquid) {
|
||||||
|
this.registerFilter('t', x => Liquid.isFalsy(x))
|
||||||
|
})
|
||||||
|
const html = await engine.parseAndRender('{{false|t}}')
|
||||||
|
expect(html).to.equal('true')
|
||||||
|
})
|
||||||
|
})
|
||||||
describe('#express()', function () {
|
describe('#express()', function () {
|
||||||
const liquid = new Liquid({root: '/root'})
|
const liquid = new Liquid({root: '/root'})
|
||||||
const render = liquid.express()
|
const render = liquid.express()
|
||||||
|
|||||||
Reference in New Issue
Block a user