mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Code improvements
This commit is contained in:
@@ -154,7 +154,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def render_node(context, output, node)
|
def render_node(context, output, node)
|
||||||
node.disabled?(context, output) && return if node.is_a?(Tag)
|
return if node.is_a?(Tag) && node.disabled?(context, output)
|
||||||
node.render_to_output_buffer(context, output)
|
node.render_to_output_buffer(context, output)
|
||||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||||
context.handle_error(e, node.line_number)
|
context.handle_error(e, node.line_number)
|
||||||
|
|||||||
@@ -2,30 +2,31 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class DisabledTags < Register
|
class DisabledTags < Register
|
||||||
def initialize
|
def initialize
|
||||||
@disabled_tags = Hash.new { |h, k| h[k] = 0 }
|
@disabled_tags = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled?(tag)
|
def disabled?(tag)
|
||||||
@disabled_tags[tag] > 0
|
@disabled_tags.key?(tag) && @disabled_tags[tag] > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable(tags)
|
def disable(tags)
|
||||||
tags.each { |tag| increment(tag) }
|
tags.each(&method(:increment))
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
tags.each { |tag| decrement(tag) }
|
tags.each(&method(:decrement))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def increment(tag)
|
def increment(tag)
|
||||||
@disabled_tags[tag] = @disabled_tags[tag] + 1
|
@disabled_tags[tag] ||= 0
|
||||||
|
@disabled_tags[tag] += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def decrement(tag)
|
def decrement(tag)
|
||||||
@disabled_tags[tag] = @disabled_tags[tag] - 1
|
@disabled_tags[tag] -= 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_register('disabled_tags', DisabledTags.new)
|
Template.add_register('disabled_tags', DisabledTags.new)
|
||||||
end
|
end
|
||||||
|
|||||||
+4
-6
@@ -40,18 +40,16 @@ module Liquid
|
|||||||
|
|
||||||
def disabled?(context, output)
|
def disabled?(context, output)
|
||||||
if context.registers['disabled_tags']&.disabled?(tag_name)
|
if context.registers['disabled_tags']&.disabled?(tag_name)
|
||||||
output << disabled_response
|
output << disabled_error_message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def disabled_response
|
def disabled_error_message
|
||||||
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable_tags(context, tags)
|
def disable_tags(context, tags, &block)
|
||||||
context.registers['disabled_tags'].disable(tags) do
|
context.registers['disabled_tags'].disable(tags, &block)
|
||||||
yield
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# For backwards compatibility with custom tags. In a future release, the semantics
|
# For backwards compatibility with custom tags. In a future release, the semantics
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||||
|
DISABLED_TAGS = %w(include)
|
||||||
|
|
||||||
attr_reader :template_name_expr, :attributes
|
attr_reader :template_name_expr, :attributes
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
disable_tags(context, %w(include)) do
|
disable_tags(context, DISABLED_TAGS) do
|
||||||
render_tag(context, output)
|
render_tag(context, output)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ module Liquid
|
|||||||
@tags ||= TagRegistry.new
|
@tags ||= TagRegistry.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_register(name, klass)
|
def add_register(name, klass)
|
||||||
registers[name.to_s] = klass
|
registers[name.to_s] = klass
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user