Improvements to disable tag

This commit is contained in:
Mike Angell
2019-09-23 20:45:09 +10:00
parent 6f823891bc
commit 05234ef6de
5 changed files with 22 additions and 8 deletions
+1 -1
View File
@@ -26,4 +26,4 @@
argument: argument:
include: "Argument error in tag 'include' - Illegal template name" include: "Argument error in tag 'include' - Illegal template name"
disabled: disabled:
include: "'include' usage has been disabled in this context, consider using 'render'." tag: "usage has been disabled in this context."
+4 -4
View File
@@ -10,19 +10,19 @@ module Liquid
end end
def disable(tag) def disable(tag)
incr(tag) increment(tag)
yield yield
ensure ensure
decr(tag) decrement(tag)
end end
private private
def incr(tag) def increment(tag)
@disabled_tags[tag] = @disabled_tags[tag] + 1 @disabled_tags[tag] = @disabled_tags[tag] + 1
end end
def decr(tag) def decrement(tag)
@disabled_tags[tag] = @disabled_tags[tag] - 1 @disabled_tags[tag] = @disabled_tags[tag] - 1
end end
end end
+8
View File
@@ -38,6 +38,14 @@ module Liquid
'' ''
end end
def disabled?(context)
context.registers['disabled_tags']&.disabled?(tag_name)
end
def disabled_response
"#{tag_name} #{options[:locale].t("errors.disabled.tag")}"
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
# of the `render_to_output_buffer` method will become the default and the `render` # of the `render_to_output_buffer` method will become the default and the `render`
# method will be removed. # method will be removed.
+1 -1
View File
@@ -45,7 +45,7 @@ module Liquid
end end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
return output << options[:locale].t("errors.disabled.include") if context.registers['disabled_tags']&.disabled?('include') return output << disabled_response if disabled?(context)
template_name = context.evaluate(@template_name_expr) template_name = context.evaluate(@template_name_expr)
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
+8 -2
View File
@@ -199,19 +199,25 @@ module Liquid
output = nil output = nil
context_register = context.registers.is_a?(StaticRegisters) ? context.registers.static : context.registers
case args.last case args.last
when Hash when Hash
options = args.pop options = args.pop
output = options[:output] if options[:output] output = options[:output] if options[:output]
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash) options[:registers]&.each do |key, register|
context_register[key] = register
end
apply_options_to_context(context, options) apply_options_to_context(context, options)
when Module, Array when Module, Array
context.add_filters(args.pop) context.add_filters(args.pop)
end end
registers.merge!(Template.registers) if Template.registers.is_a?(Hash) Template.registers.each do |key, register|
context_register[key] = register
end
# Retrying a render resets resource usage # Retrying a render resets resource usage
context.resource_limits.reset context.resource_limits.reset