Skip to content

Render an Object as a String in Final Template but Pass the Full Object to Tags #755

Description

@ErikAhlund

I’m currently using LiquidJS and I’m trying to implement the following behavior:

  1. I want to pass an object to a custom tag and receive the full object in the tag.
  2. If the same object is rendered in the final template (i.e., without being passed to a tag), I want to retrieve a string by looking up data from a database and returning a specific value.

Example Scenario:
Let’s say I have an object structured like this:

{
  "Value": "12345",
  "Type": "user"
}
  • If this object is passed to a custom tag, I expect the tag to receive the entire object so that I can use the Value and Type inside the tag function.
  • However, if this object is directly rendered in the template (i.e., via {{ object }}), I want it to trigger a lookup in the database (in this case, using the Value field to find the firstName and lastName of the user) and return a string with the user's name.

Specific Questions:

Is it possible to achieve this behaviour using Drops and toLiquid() and/or valueOf() in LiquidJS?

  1. I tried returning an object in the toLiquid() function, but the only way that worked was by defining a key. So if I defined name to be the function that does the db lookup, then when rendering {{ object.name }} it worked. But I want to have this behaviour without having to add ".name" and simply have {{ object }}.
  2. What happens when an object is set using the {% capture %} tag? If I use the {% capture %} tag to store this object in a variable, will the captured variable hold the entire object (with its Value and Type fields), or will it contain the rendered string (the user's name after the database lookup)?
{% capture userVar %}
  {{ object }}
{% endcapture %}

Would userVar hold the object or the rendered string (e.g., "John Doe")?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions