Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update variable's context before invoking its to_liquid #1781

Merged
merged 2 commits into from Feb 12, 2024

Conversation

ggmichaelgo
Copy link
Contributor

@ggmichaelgo ggmichaelgo commented Jan 24, 2024

What are you trying to solve?

Liquid-C PR: Shopify/liquid-c#212 (Liquid-C PR needs to be merged first)

On variable lookups, Context is invoking the drop's to_liquid function, and update the values context to itself.

This becomes problematic if the drop's to_liquid has some contextual checks:

require 'liquid'

class ProductsDrop < Liquid::Drop
  def initialize(products)
    @products = products
  end

  def size
    @products.size
  end

  def to_liquid
    if @context["forloop"]
      @products.first(@context["forloop"].length)
    else
      @products
    end
  end
end

context = {
  "products" => ProductsDrop.new(["A", "B", "C", "D", "E"]),
}

template = Liquid::Template.parse(<<~LIQUID)
  {% for i in (1..3) %}
    for-loop: {{ products | size }} # returns 3
  {% endfor %}

  {{ products }} # it still returns 3 instead of 5
LIQUID

puts template.render(context)

We could solve issue by updating the Context#find_variable to update the drop's context before invoking its to_liquid method.

@ggmichaelgo ggmichaelgo force-pushed the contextualize-before-to-liquid branch 5 times, most recently from 47a04ff to b2905a8 Compare January 25, 2024 21:33
@ggmichaelgo ggmichaelgo requested review from a team, ianks and Maaarcocr and removed request for a team January 26, 2024 19:21
@ggmichaelgo
Copy link
Contributor Author

ping @Shopify/guardians-of-the-liquid

@ggmichaelgo ggmichaelgo marked this pull request as ready for review February 12, 2024 20:50
@ggmichaelgo ggmichaelgo merged commit 4a4fe3c into main Feb 12, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants