mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 09:15:41 +03:00
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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user