Allow passing arbitrary JavaScript objects as context, or introduce a separate hash for contextual data
See original GitHub issueBasic example:
const fs = require('fs')
liquidjs.parseAndRender(layoutContent, {key: 'val', __fs: fs}) // option 1 - part of regular context
liquidjs.parseAndRender(layoutContent, {key: 'val'}, {fs}) // option 2 - third parameter for context data not exposed as {{ variables }}, accessible programatically in tags/filters only
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:
engine.registerTag('asset', {
// ...
render: async function (context, emitter, scope, jsContext) { // jsContext, if option 2 is picked
const assetNode = {
children: [],
id: gatsbyApi.createNodeId(`asset-${this.input}`),
internal: {
type: 'JekyllAsset',
contentDigest: gatsbyApi.createContentDigest(this.input),
},
outputPath: this.output,
originalPath: this.originalPath,
}
actions.createNode(assetNode)
actions.createParentChildLink({
parent: context.getAll().__node, // or jsContext, if option 2 is picked
child: assetNode
})
}
}
// Then use it in createPages hook:
liquidjs.parseAndRender(template, {page, site, node})
This allows me to perform sophisticated actions inside tags/filters - not just to return a value.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Is there hash code function accepting any object type?
I've written a small library that creates hashes from objects, which you can easily use for this purpose. The objects can even have...
Read more >Understanding Context in JavaScript - Object Literals
Learn how the “this” keyword works within Object Literals, and inside functions that are executed in the context of Object Literals.
Read more >JavaScript Hash Table – Associative Array Hashing in JS
Hash Tables are a data structure that allow you to create a list of paired values. You can then retrieve a certain value...
Read more >Node.js v19.3.0 Documentation
args <any> Optional arguments to pass to the function. Call the provided function with the provided arguments in the execution context of the...
Read more >JNDI SPI
The SPI provides methods that allow different provider implementations to cooperate to complete client JNDI operations. This document describes the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, sorry, I forgot to close this. Yeah, it came from somewhere else, not from Liquidjs.
After digging into this a bit, I find the “cannot convert object to primitive value” happens when implicitly converting objects without
toString
method tostring
. LikeI checked the codebase and didn’t find similiar expressions. I tried the following code which didn’t throw such an error:
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”.