access scope in liquidjs filter
HTML
<script src="https://cdn.jsdelivr.net/npm/liquidjs/dist/liquid.browser.min.js"></script>
<div id="log"></div>
JavaScript
const { Liquid, Hash } = liquidjs
const engine = new Liquid()
engine.registerTag('random', {
parse(tagToken) {
this.args = new Hash(tagToken.args)
},
* render(context, emitter) {
const {from, to} = yield this.args.render(context)
const length = to - from + 1
const value = from + Math.floor(length * Math.random())
emitter.write(value)
}
})
const tpl = `{% random from:2, to:max %}`
const scope = { max: 10 }
engine.parseAndRender(tpl, scope).then(html => log(html))
function log(text) {
document.querySelector("#log").innerText += text + '\n';
}