mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
@@ -13,7 +13,7 @@ module Liquid
|
|||||||
# context['bob'] #=> nil class Context
|
# context['bob'] #=> nil class Context
|
||||||
class Context
|
class Context
|
||||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
||||||
attr_accessor :exception_handler, :template_name, :partial
|
attr_accessor :exception_handler, :template_name, :partial, :global_filter
|
||||||
|
|
||||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
||||||
@environments = [environments].flatten
|
@environments = [environments].flatten
|
||||||
@@ -32,6 +32,7 @@ module Liquid
|
|||||||
|
|
||||||
@interrupts = []
|
@interrupts = []
|
||||||
@filters = []
|
@filters = []
|
||||||
|
@global_filter = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def warnings
|
def warnings
|
||||||
@@ -52,6 +53,10 @@ module Liquid
|
|||||||
@strainer = nil
|
@strainer = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def apply_global_filter(obj)
|
||||||
|
global_filter.nil? ? obj : global_filter.call(obj)
|
||||||
|
end
|
||||||
|
|
||||||
# are there any not handled interrupts?
|
# are there any not handled interrupts?
|
||||||
def interrupt?
|
def interrupt?
|
||||||
!@interrupts.empty?
|
!@interrupts.empty?
|
||||||
|
|||||||
+7
-12
@@ -179,20 +179,15 @@ module Liquid
|
|||||||
when Hash
|
when Hash
|
||||||
options = args.pop
|
options = args.pop
|
||||||
|
|
||||||
if options[:registers].is_a?(Hash)
|
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
|
||||||
registers.merge!(options[:registers])
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:filters]
|
context.add_filters(options[:filters]) if options[:filters]
|
||||||
context.add_filters(options[:filters])
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:exception_handler]
|
context.global_filter = options[:global_filter] if options[:global_filter]
|
||||||
context.exception_handler = options[:exception_handler]
|
|
||||||
end
|
context.exception_handler = options[:exception_handler] if options[:exception_handler]
|
||||||
when Module
|
|
||||||
context.add_filters(args.pop)
|
when Module, Array
|
||||||
when Array
|
|
||||||
context.add_filters(args.pop)
|
context.add_filters(args.pop)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -73,10 +73,16 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
@filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
|
obj = @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
|
||||||
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
|
||||||
context.invoke(filter_name, output, *filter_args)
|
context.invoke(filter_name, output, *filter_args)
|
||||||
end.tap{ |obj| taint_check(context, obj) }
|
end
|
||||||
|
|
||||||
|
obj = context.apply_global_filter(obj)
|
||||||
|
|
||||||
|
taint_check(context, obj)
|
||||||
|
|
||||||
|
obj
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -211,4 +211,18 @@ class TemplateTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
assert exception.is_a?(Liquid::ZeroDivisionError)
|
assert exception.is_a?(Liquid::ZeroDivisionError)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_global_filter_option_on_render
|
||||||
|
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||||
|
rendered_template = Template.parse("{{name}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
|
||||||
|
|
||||||
|
assert_equal 'bob filtered', rendered_template
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_global_filter_option_when_native_filters_exist
|
||||||
|
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||||
|
rendered_template = Template.parse("{{name | upcase}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
|
||||||
|
|
||||||
|
assert_equal 'BOB filtered', rendered_template
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -466,4 +466,18 @@ class ContextUnitTest < Minitest::Test
|
|||||||
assert contx
|
assert contx
|
||||||
assert_nil contx['poutine']
|
assert_nil contx['poutine']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_apply_global_filter
|
||||||
|
global_filter_proc = ->(output) { "#{output} filtered" }
|
||||||
|
|
||||||
|
context = Context.new
|
||||||
|
context.global_filter = global_filter_proc
|
||||||
|
|
||||||
|
assert_equal 'hi filtered', context.apply_global_filter('hi')
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_apply_global_filter_when_no_global_filter_exist
|
||||||
|
context = Context.new
|
||||||
|
assert_equal 'hi', context.apply_global_filter('hi')
|
||||||
|
end
|
||||||
end # ContextTest
|
end # ContextTest
|
||||||
|
|||||||
Reference in New Issue
Block a user