mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Merge pull request #1781 from Shopify/contextualize-before-to-liquid
update variable's context before invoking its to_liquid
This commit is contained in:
@@ -23,6 +23,6 @@ group :test do
|
|||||||
gem 'rubocop-performance', require: false
|
gem 'rubocop-performance', require: false
|
||||||
|
|
||||||
platform :mri, :truffleruby do
|
platform :mri, :truffleruby do
|
||||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'master'
|
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'main'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -197,10 +197,14 @@ module Liquid
|
|||||||
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
|
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
|
||||||
end
|
end
|
||||||
|
|
||||||
variable = variable.to_liquid
|
# update variable's context before invoking #to_liquid
|
||||||
variable.context = self if variable.respond_to?(:context=)
|
variable.context = self if variable.respond_to?(:context=)
|
||||||
|
|
||||||
variable
|
liquid_variable = variable.to_liquid
|
||||||
|
|
||||||
|
liquid_variable.context = self if variable != liquid_variable && liquid_variable.respond_to?(:context=)
|
||||||
|
|
||||||
|
liquid_variable
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_and_evaluate(obj, key, raise_on_not_found: true)
|
def lookup_and_evaluate(obj, key, raise_on_not_found: true)
|
||||||
|
|||||||
@@ -36,6 +36,24 @@ class Category
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
class CategoryDrop < Liquid::Drop
|
class CategoryDrop < Liquid::Drop
|
||||||
attr_accessor :category, :context
|
attr_accessor :category, :context
|
||||||
|
|
||||||
@@ -635,6 +653,25 @@ class ContextTest < Minitest::Test
|
|||||||
assert_equal(:my_value, c.registers[:my_register])
|
assert_equal(:my_value, c.registers[:my_register])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_variable_to_liquid_returns_contextual_drop
|
||||||
|
context = {
|
||||||
|
"products" => ProductsDrop.new(["A", "B", "C", "D", "E"]),
|
||||||
|
}
|
||||||
|
|
||||||
|
template = Liquid::Template.parse(<<~LIQUID)
|
||||||
|
{%- for i in (1..3) -%}
|
||||||
|
for_loop_products_count: {{ products | size }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
unscoped_products_count: {{ products | size }}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
result = template.render(context)
|
||||||
|
|
||||||
|
assert_includes(result, "for_loop_products_count: 3")
|
||||||
|
assert_includes(result, "unscoped_products_count: 5")
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def assert_no_object_allocations
|
def assert_no_object_allocations
|
||||||
|
|||||||
Reference in New Issue
Block a user