mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 00:40:40 -07:00
Include template name with line numbers in render errors.
This commit is contained in:
@@ -13,7 +13,7 @@ module Liquid
|
||||
# context['bob'] #=> nil class Context
|
||||
class Context
|
||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
||||
attr_accessor :exception_handler, :render_errors
|
||||
attr_accessor :exception_handler, :render_errors, :template_name
|
||||
|
||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, render_errors = true)
|
||||
@environments = [environments].flatten
|
||||
@@ -65,6 +65,7 @@ module Liquid
|
||||
|
||||
def handle_error(e, token = nil)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.template_name = template_name
|
||||
e.set_line_number_from_token(token)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module Liquid
|
||||
class Error < ::StandardError
|
||||
attr_accessor :line_number
|
||||
attr_accessor :template_name
|
||||
attr_accessor :markup_context
|
||||
|
||||
def to_s(with_prefix = true)
|
||||
@@ -41,7 +42,9 @@ module Liquid
|
||||
end
|
||||
|
||||
if line_number
|
||||
str << " (line #{line_number})"
|
||||
str << " ("
|
||||
str << template_name << " " if template_name
|
||||
str << "line " << line_number.to_s << ")"
|
||||
end
|
||||
|
||||
str << ": "
|
||||
|
||||
+19
-14
@@ -41,9 +41,9 @@ module Liquid
|
||||
end
|
||||
|
||||
def render(context)
|
||||
partial = load_cached_partial(context)
|
||||
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
partial = load_cached_partial(template_name, context)
|
||||
|
||||
context_variable_name = template_name.split('/'.freeze).last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
@@ -52,28 +52,33 @@ module Liquid
|
||||
context.find_variable(template_name)
|
||||
end
|
||||
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
context[key] = context.evaluate(value)
|
||||
end
|
||||
old_template_name = context.template_name
|
||||
begin
|
||||
context.template_name = template_name
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
context[key] = context.evaluate(value)
|
||||
end
|
||||
|
||||
if variable.is_a?(Array)
|
||||
variable.collect do |var|
|
||||
context[context_variable_name] = var
|
||||
if variable.is_a?(Array)
|
||||
variable.collect do |var|
|
||||
context[context_variable_name] = var
|
||||
partial.render(context)
|
||||
end
|
||||
else
|
||||
context[context_variable_name] = variable
|
||||
partial.render(context)
|
||||
end
|
||||
else
|
||||
context[context_variable_name] = variable
|
||||
partial.render(context)
|
||||
end
|
||||
ensure
|
||||
context.template_name = old_template_name
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_cached_partial(context)
|
||||
def load_cached_partial(template_name, context)
|
||||
cached_partials = context.registers[:cached_partials] || {}
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
|
||||
if cached = cached_partials[template_name]
|
||||
return cached
|
||||
|
||||
Reference in New Issue
Block a user