From 71ff1f283a601ee27d41c84e8e78eaeefbd5b94e Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 21 May 2015 10:49:21 -0400 Subject: [PATCH] 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. --- lib/liquid/context.rb | 7 ++++--- lib/liquid/template.rb | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 3314eee..c5e9f8a 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -13,15 +13,16 @@ module Liquid # context['bob'] #=> nil class Context class Context 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 @scopes = [(outer_scope || {})] @registers = registers @errors = [] @resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits) squash_instance_assigns_with_environments + @render_errors = render_errors @this_stack_used = false @@ -69,7 +70,7 @@ module Liquid errors.push(e) raise if exception_handler && exception_handler.call(e) - Liquid::Error.render(e) + render_errors ? Liquid::Error.render(e) : '' end def invoke(method, *args) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 88d9977..a14a331 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -17,7 +17,7 @@ module Liquid locale: I18n.new } - attr_accessor :root + attr_accessor :root, :render_errors attr_reader :resource_limits @@file_system = BlankFileSystem.new @@ -112,6 +112,7 @@ module Liquid def initialize @resource_limits = ResourceLimits.new(self.class.default_resource_limits) + @render_errors = true end # Parse source code. @@ -163,6 +164,8 @@ module Liquid def render(*args) return ''.freeze if @root.nil? + render_errors = self.render_errors + context = case args.first when Liquid::Context c = args.shift @@ -174,11 +177,11 @@ module Liquid c when Liquid::Drop 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 - 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 - Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits) + Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits, render_errors) else raise ArgumentError, "Expected Hash or Liquid::Context as parameter" end