Closed Nowaker closed 2 years ago
Why not just expose it as ordinary scope variables? Scope is designed to carry variables for filter use.
If you need to expose it to all filters without explicitly pass to them, there's a globals option:
liquidjs.parseAndRender(tpl, scope, {globals})
Ok, I'll check that, but the way I understand globals, it's no different than passing it in scope, which requires primitive values only.
Sorry I missed this error, I suppose it's not expected. Do you know where it's from?
Error: Cannot convert object to primitive value
After digging into this a bit, I find the "cannot convert object to primitive value" happens when implicitly converting objects without toString
method to string
. Like
Object.create(null) + ''
I checked the codebase and didn't find similiar expressions. I tried the following code which didn't throw such an error:
const fs = require('fs')
const { Liquid } = require('.')
const engine = new Liquid();
// [object Object]
console.log(engine.parseAndRenderSync('{{fs}}', {fs}))
// [object Object]foo
console.log(engine.parseAndRenderSync('{{fs | append: "foo"}}', {fs}))
Closing this issue since LiquidJS actually allows arbitrary JavaScript objects as contexts. Feel free to open another issue with a runnable snippet if somewhere in LiquidJS throws "Error: Cannot convert object to primitive value".
Ah, sorry, I forgot to close this. Yeah, it came from somewhere else, not from Liquidjs.
Basic example:
Currently:
Error: Cannot convert object to primitive value
Reasoning: I'd like to pass some contextual data to every
parseAndRender
call that I would access in tag/filter handlers.Sophisticated example in Gatsby:
This allows me to perform sophisticated actions inside tags/filters - not just to return a value.