mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 00:55:11 -07:00
Improve disable tag API
This commit is contained in:
@@ -155,7 +155,9 @@ module Liquid
|
|||||||
|
|
||||||
def render_node(context, output, node)
|
def render_node(context, output, node)
|
||||||
return if node.is_a?(Tag) && node.disabled?(context, output)
|
return if node.is_a?(Tag) && node.disabled?(context, output)
|
||||||
|
disable_tags(context, node.is_a?(Tag) ? node.disabled_tags : nil) do
|
||||||
node.render_to_output_buffer(context, output)
|
node.render_to_output_buffer(context, output)
|
||||||
|
end
|
||||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||||
context.handle_error(e, node.line_number)
|
context.handle_error(e, node.line_number)
|
||||||
rescue ::StandardError => e
|
rescue ::StandardError => e
|
||||||
@@ -163,6 +165,11 @@ module Liquid
|
|||||||
output << context.handle_error(e, line_number)
|
output << context.handle_error(e, line_number)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_tags(context, tags, &block)
|
||||||
|
return yield unless tags
|
||||||
|
context.registers['disabled_tags'].disable(tags, &block)
|
||||||
|
end
|
||||||
|
|
||||||
def raise_if_resource_limits_reached(context, length)
|
def raise_if_resource_limits_reached(context, length)
|
||||||
context.resource_limits.render_length += length
|
context.resource_limits.render_length += length
|
||||||
return unless context.resource_limits.reached?
|
return unless context.resource_limits.reached?
|
||||||
|
|||||||
+12
-4
@@ -13,7 +13,15 @@ module Liquid
|
|||||||
tag
|
tag
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_nested_tags(*tags)
|
||||||
|
@disabled_tags = tags
|
||||||
|
end
|
||||||
|
|
||||||
private :new
|
private :new
|
||||||
|
|
||||||
|
def disabled_tags
|
||||||
|
@disabled_tags ||= []
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(tag_name, markup, parse_context)
|
def initialize(tag_name, markup, parse_context)
|
||||||
@@ -48,10 +56,6 @@ module Liquid
|
|||||||
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def disable_tags(context, tags, &block)
|
|
||||||
context.registers['disabled_tags'].disable(tags, &block)
|
|
||||||
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.
|
||||||
@@ -63,5 +67,9 @@ module Liquid
|
|||||||
def blank?
|
def blank?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disabled_tags
|
||||||
|
self.class.disabled_tags
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||||
DISABLED_TAGS = %w(include).freeze
|
|
||||||
|
disable_nested_tags "include"
|
||||||
|
|
||||||
attr_reader :template_name_expr, :attributes
|
attr_reader :template_name_expr, :attributes
|
||||||
|
|
||||||
@@ -23,10 +24,8 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
disable_tags(context, DISABLED_TAGS) do
|
|
||||||
render_tag(context, output)
|
render_tag(context, output)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def render_tag(context, output)
|
def render_tag(context, output)
|
||||||
# Though we evaluate this here we will only ever parse it as a string literal.
|
# Though we evaluate this here we will only ever parse it as a string literal.
|
||||||
|
|||||||
@@ -6,19 +6,11 @@ class DisabledTagsTest < Minitest::Test
|
|||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
class DisableRaw < Block
|
class DisableRaw < Block
|
||||||
def render(context)
|
disable_nested_tags "raw"
|
||||||
disable_tags(context, ["raw"]) do
|
|
||||||
@body.render(context)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class DisableRawEcho < Block
|
class DisableRawEcho < Block
|
||||||
def render(context)
|
disable_nested_tags "raw", "echo"
|
||||||
disable_tags(context, ["raw", "echo"]) do
|
|
||||||
@body.render(context)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_disables_raw
|
def test_disables_raw
|
||||||
|
|||||||
Reference in New Issue
Block a user