Skip to content
2 changes: 1 addition & 1 deletion lib/liquid/block_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def render_to_output_buffer(context, output)

case node
when String
output << node
output.force_encoding('UTF-8') << node.force_encoding('UTF-8')
when Variable
render_node(context, output, node)
when Block
Expand Down
7 changes: 6 additions & 1 deletion lib/liquid/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def disabled_error_message
# of the `render_to_output_buffer` method will become the default and the `render`
# method will be removed.
def render_to_output_buffer(context, output)
output << render(context)
rendered = render(context)
if output.nil?
output = rendered.to_s
else
output << rendered.to_s
end
output
end

Expand Down
19 changes: 18 additions & 1 deletion lib/liquid/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,31 @@ def render(context)
def render_to_output_buffer(context, output)
obj = render(context)

if output.nil?
output = +''
elsif output.frozen?
raise LiquidError, "Cannot mutate frozen output buffer"
end

if obj.is_a?(Array)
output << obj.join
elsif obj.kind_of?(Hash)
output << obj.to_s
elsif obj.nil?
# do nothing
elsif obj.kind_of?(String)
output << obj
else
output << obj.to_s
output << obj.try(:to_s) || ""
end

output
rescue Encoding::CompatibilityError => e
if obj.kind_of?(String)
output.force_encoding('UTF-8') << obj
else
raise e
end
end

def disabled?(_context)
Expand Down