Shopify / Shopify/liquid

Access other tags of the same class from a custom liquid tag

Open
#1,682 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

I'm building a custom liquid tag which is used to build nodes in a tree. The tag takes a level argument, which affects its indentation in the tree. Pretty basic, but it works.

I would, however, like to make it a bit more complex by drawing lines between the nodes. To do this, I not only need to know the level of each node, but also whether it has children and/or siblings.

I could solve this by adding both has_siblings and has_children parameters to each node, but if the tree has many nodes, it quickly becomes unwieldy and hard to maintain. It would therefore be preferable to be able to programmatically find other, adjacent (formerly processed) liquid tags of the same class, and update their has_sibling and has_children fields as required. The question then becomes:

  1. How do I find adjacent nodes (custom Liquid tags) of the same class?
  2. Where do I store the required data for each node?

I've tried to poke at Liquid::ParseContext, but haven't found anything useful to do with it. I can see that the same ParseContext instance is shared between each tag instance, but ParseContext does not seem to expose a dictionary I can add items to.

I've thought that instead of using the ParseContext for state keeping, I can add a @@class_field to my custom tag class, but then I need to partition each instantiation of the tag by filename and vicinity somehow (I suppose Liquid::ParseContext#line_number can be used here). filename does not seem to be exposed by ParseContext.

I'm pretty sure I'm not going in the right direction here or missing something obvious, so some guidance would be highly appreciated! 😅 🙏🏼

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how custom Liquid tags are instantiated and how Liquid::ParseContext is shared, including its line_number and filename-related access. Determine whether adjacent tag state can be exposed safely across parsing contexts; done would be a documented, supported way to access or store data for related custom tags.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.