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

View File

@@ -26,4 +26,4 @@
argument:
include: "Argument error in tag 'include' - Illegal template name"
disabled:
include: "'include' usage has been disabled in this context, consider using 'render'."
tag: "usage has been disabled in this context."

View File

@@ -10,19 +10,19 @@ module Liquid
end
def disable(tag)
incr(tag)
increment(tag)
yield
ensure
decr(tag)
decrement(tag)
end
private
def incr(tag)
def increment(tag)
@disabled_tags[tag] = @disabled_tags[tag] + 1
end
def decr(tag)
def decrement(tag)
@disabled_tags[tag] = @disabled_tags[tag] - 1
end
end

View File

@@ -38,6 +38,14 @@ module Liquid
''
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
# of the `render_to_output_buffer` method will become the default and the `render`
# method will be removed.

View File

@@ -45,7 +45,7 @@ module Liquid
end
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)
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name

View File

@@ -199,19 +199,25 @@ module Liquid
output = nil
context_register = context.registers.is_a?(StaticRegisters) ? context.registers.static : context.registers
case args.last
when Hash
options = args.pop
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)
when Module, Array
context.add_filters(args.pop)
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
context.resource_limits.reset