question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow passing arbitrary JavaScript objects as context, or introduce a separate hash for contextual data

See original GitHub issue

Basic 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Nowakercommented, Apr 21, 2022

Ah, sorry, I forgot to close this. Yeah, it came from somewhere else, not from Liquidjs.

0reactions
harttlecommented, Apr 21, 2022

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”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found