Replies: 5 comments
-
Based on https://github.com/harttle/liquidjs/blob/master/src/builtin/filters/date.ts#L8 I can see engine.registerFilter('liquify', (...args) => {
return `ctx:${this.context}`
}) Because the source is in TypeScript, and I use Liquidjs in JS, I don't really know how one converts to the other. There's this FilterImpl that appears to have what I want but I've no idea how to access it in filters the way it's currently documented (pure JS code). Another thing worth documenting is how to use asynchronous calls in filters. It took me a while to figure it out: engine.registerFilter('liquify', async (...args) => {
context = {key: 'val'} // TODO: Pass context. https://github.com/harttle/liquidjs/issues/444
const html = await engine.parseAndRender(args[0], context).then((results) => {
return results
})
return html;
}) |
Beta Was this translation helpful? Give feedback.
-
The filter need to be declared by engine.registerFilter('liquify', function(...args) {
return `ctx:${this.context}`
}) We should highlight this caveat in filter docs. Update: create a tutorial to highlight this part, https://liquidjs.com/tutorials/access-scope-in-filters.html |
Beta Was this translation helpful? Give feedback.
-
@harttle Thank you! This works perfectly: engine.registerFilter('liquify', async function(...args) {
const syntax = args[0]
const template = this.liquid.parse(syntax)
const result = this.liquid.renderer.renderTemplates(template, this.context)
const html = await toPromise(result)
return html
}) BTW, I had to use this, and not {% assign tldarray = "club|io|online|org|xyz|com" | split: "|" %}
{% for tld in tldarray %}
{% tag /domains/{{tld}}/ %}
|
Beta Was this translation helpful? Give feedback.
-
I see. Calling low-level Raw So |
Beta Was this translation helpful? Give feedback.
-
@harttle Great, |
Beta Was this translation helpful? Give feedback.
-
I need to re-create a custom Jekyll filter
liquify
(https://github.com/gemfarmer/jekyll-liquify/blob/master/lib/jekyll-liquify.rb):This is an equivalent of
eval
in Liquid:I am not sure if this is even possible because the documentation doesn't specify how to access the context. https://liquidjs.com/tutorials/register-filters-tags.html#Register-Filters
If it's possible to access the context within a filter, please explain how in the docs. Otherwise, please consider making it possible to access the context within a filter - just like in tags (https://liquidjs.com/tutorials/register-filters-tags.html#Register-Tags).
CC @harttle
Beta Was this translation helpful? Give feedback.
All reactions