Make liquid rendering optional.

Although the author of the liquid template wants to see these errors, they
probably don't want the visitor to see the liquid errors.  Probably the
best fallback when rendering the page for visitors is to render the empty
string for tags with errors.
This commit is contained in:
Dylan Thacker-Smith
2015-05-21 10:49:32 -04:00
parent 070639daba
commit 71ff1f283a
2 changed files with 11 additions and 7 deletions
+4 -3
View File
@@ -13,15 +13,16 @@ 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 attr_accessor :exception_handler, :render_errors
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil) def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, render_errors = true)
@environments = [environments].flatten @environments = [environments].flatten
@scopes = [(outer_scope || {})] @scopes = [(outer_scope || {})]
@registers = registers @registers = registers
@errors = [] @errors = []
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits) @resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
squash_instance_assigns_with_environments squash_instance_assigns_with_environments
@render_errors = render_errors
@this_stack_used = false @this_stack_used = false
@@ -69,7 +70,7 @@ module Liquid
errors.push(e) errors.push(e)
raise if exception_handler && exception_handler.call(e) raise if exception_handler && exception_handler.call(e)
Liquid::Error.render(e) render_errors ? Liquid::Error.render(e) : ''
end end
def invoke(method, *args) def invoke(method, *args)
+7 -4
View File
@@ -17,7 +17,7 @@ module Liquid
locale: I18n.new locale: I18n.new
} }
attr_accessor :root attr_accessor :root, :render_errors
attr_reader :resource_limits attr_reader :resource_limits
@@file_system = BlankFileSystem.new @@file_system = BlankFileSystem.new
@@ -112,6 +112,7 @@ module Liquid
def initialize def initialize
@resource_limits = ResourceLimits.new(self.class.default_resource_limits) @resource_limits = ResourceLimits.new(self.class.default_resource_limits)
@render_errors = true
end end
# Parse source code. # Parse source code.
@@ -163,6 +164,8 @@ module Liquid
def render(*args) def render(*args)
return ''.freeze if @root.nil? return ''.freeze if @root.nil?
render_errors = self.render_errors
context = case args.first context = case args.first
when Liquid::Context when Liquid::Context
c = args.shift c = args.shift
@@ -174,11 +177,11 @@ module Liquid
c c
when Liquid::Drop when Liquid::Drop
drop = args.shift drop = args.shift
drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, render_errors)
when Hash when Hash
Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, render_errors)
when nil when nil
Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits) Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits, render_errors)
else else
raise ArgumentError, "Expected Hash or Liquid::Context as parameter" raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
end end