mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Update context tags to act as a merge rather than a complete overwrite
This commit is contained in:
@@ -2,15 +2,25 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class ParseContext
|
class ParseContext
|
||||||
attr_accessor :locale, :line_number, :trim_whitespace, :depth, :tags
|
attr_accessor :locale, :line_number, :trim_whitespace, :depth
|
||||||
attr_reader :partial, :warnings, :error_mode
|
attr_reader :partial, :warnings, :error_mode, :tags
|
||||||
|
|
||||||
|
class Tags
|
||||||
|
def initialize(tags)
|
||||||
|
@tags = tags || {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](tag_name)
|
||||||
|
@tags[tag_name] || Liquid::Template.tags[tag_name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@template_options = options ? options.dup : {}
|
@template_options = options ? options.dup : {}
|
||||||
|
|
||||||
@locale = @template_options[:locale] ||= I18n.new
|
@locale = @template_options[:locale] ||= I18n.new
|
||||||
@warnings = []
|
@warnings = []
|
||||||
@tags = @template_options[:tags] || Liquid::Template.tags
|
@tags = Tags.new(@template_options[:tags])
|
||||||
|
|
||||||
self.depth = 0
|
self.depth = 0
|
||||||
self.partial = false
|
self.partial = false
|
||||||
|
|||||||
@@ -44,31 +44,37 @@ class TagTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_tags_can_be_overwritten_using_parse_context
|
def test_tags_can_be_overwritten_using_parse_context
|
||||||
tag_name = 'testtag'
|
static_tag = Class.new(Tag) do
|
||||||
|
def render(*)
|
||||||
|
'static_tag'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
original_tag = Class.new(Block) do
|
original_tag = Class.new(Tag) do
|
||||||
def render(*)
|
def render(*)
|
||||||
'original_tag'
|
'original_tag'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
new_tag = Class.new(Block) do
|
new_tag = Class.new(Tag) do
|
||||||
def render(*)
|
def render(*)
|
||||||
'new_tag'
|
'new_tag'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tags_overwrite = Liquid::Template::TagRegistry.new
|
tags_overwrite = Liquid::Template::TagRegistry.new
|
||||||
tags_overwrite[tag_name] = new_tag
|
tags_overwrite['dynamic_tag'] = new_tag
|
||||||
|
|
||||||
with_custom_tag(tag_name, original_tag) do
|
with_custom_tag('static_tag', static_tag) do
|
||||||
liquid = "{% #{tag_name} %} {% end#{tag_name} %}"
|
with_custom_tag('dynamic_tag', original_tag) do
|
||||||
|
liquid = '{% static_tag %} {% dynamic_tag %}'
|
||||||
|
|
||||||
template = Liquid::Template.parse(liquid)
|
template = Liquid::Template.parse(liquid)
|
||||||
assert_equal('original_tag', template.render)
|
assert_equal('static_tag original_tag', template.render)
|
||||||
|
|
||||||
template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite)
|
template_with_overwrite = Liquid::Template.parse(liquid, tags: tags_overwrite)
|
||||||
assert_equal('new_tag', template_with_overwrite.render)
|
assert_equal('static_tag new_tag', template_with_overwrite.render)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user