diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 2dcc6af..b341a31 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -25,8 +25,6 @@ module Liquid @resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits) squash_instance_assigns_with_environments - @this_stack_used = false - self.exception_renderer = Template.default_exception_renderer if rethrow_errors self.exception_renderer = ->(e) { raise } @@ -111,19 +109,26 @@ module Liquid # end # # context['var] #=> nil - def stack(new_scope = nil) - old_stack_used = @this_stack_used - if new_scope - push(new_scope) - @this_stack_used = true - else - @this_stack_used = false - end - + # + # false or {} can be used to control if a new scope is needed + # + # Example: + # new_scope = false + # context.stack(new_scope) do + # # no scope created + # end + # + # Example: + # new_scope = {} + # context.stack(new_scope) do + # # scope created + # end + # + def stack(new_scope = {}) + push(new_scope) unless new_scope == false yield ensure - pop if @this_stack_used - @this_stack_used = old_stack_used + pop unless new_scope == false end def clear_instance_assigns @@ -132,10 +137,6 @@ module Liquid # Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop def []=(key, value) - unless @this_stack_used - @this_stack_used = true - push({}) - end @scopes[0][key] = value end diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index 92b2ed0..e167b9d 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -39,16 +39,14 @@ module Liquid end def render_to_output_buffer(context, output) - context.stack do - execute_else_block = true + execute_else_block = true - @blocks.each do |block| - if block.else? - block.attachment.render_to_output_buffer(context, output) if execute_else_block - elsif block.evaluate(context) - execute_else_block = false - block.attachment.render_to_output_buffer(context, output) - end + @blocks.each do |block| + if block.else? + block.attachment.render_to_output_buffer(context, output) if execute_else_block + elsif block.evaluate(context) + execute_else_block = false + block.attachment.render_to_output_buffer(context, output) end end diff --git a/lib/liquid/tags/cycle.rb b/lib/liquid/tags/cycle.rb index e42244d..8c11d37 100644 --- a/lib/liquid/tags/cycle.rb +++ b/lib/liquid/tags/cycle.rb @@ -34,25 +34,23 @@ module Liquid def render_to_output_buffer(context, output) context.registers[:cycle] ||= {} - context.stack do - key = context.evaluate(@name) - iteration = context.registers[:cycle][key].to_i + key = context.evaluate(@name) + iteration = context.registers[:cycle][key].to_i - val = context.evaluate(@variables[iteration]) + val = context.evaluate(@variables[iteration]) - if val.is_a?(Array) - val = val.join - elsif !val.is_a?(String) - val = val.to_s - end - - output << val - - iteration += 1 - iteration = 0 if iteration >= @variables.size - context.registers[:cycle][key] = iteration + if val.is_a?(Array) + val = val.join + elsif !val.is_a?(String) + val = val.to_s end + output << val + + iteration += 1 + iteration = 0 if iteration >= @variables.size + context.registers[:cycle][key] = iteration + output end diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 25534a9..709cf7f 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -40,11 +40,9 @@ module Liquid end def render_to_output_buffer(context, output) - context.stack do - @blocks.each do |block| - if block.evaluate(context) - return block.attachment.render_to_output_buffer(context, output) - end + @blocks.each do |block| + if block.evaluate(context) + return block.attachment.render_to_output_buffer(context, output) end end diff --git a/lib/liquid/tags/ifchanged.rb b/lib/liquid/tags/ifchanged.rb index e3040ce..ddd276c 100644 --- a/lib/liquid/tags/ifchanged.rb +++ b/lib/liquid/tags/ifchanged.rb @@ -1,14 +1,12 @@ module Liquid class Ifchanged < Block def render_to_output_buffer(context, output) - context.stack do - block_output = '' - super(context, block_output) + block_output = '' + super(context, block_output) - if block_output != context.registers[:ifchanged] - context.registers[:ifchanged] = block_output - output << block_output - end + if block_output != context.registers[:ifchanged] + context.registers[:ifchanged] = block_output + output << block_output end output diff --git a/lib/liquid/tags/unless.rb b/lib/liquid/tags/unless.rb index 18856c3..32aa3a4 100644 --- a/lib/liquid/tags/unless.rb +++ b/lib/liquid/tags/unless.rb @@ -7,18 +7,16 @@ module Liquid # class Unless < If def render_to_output_buffer(context, output) - context.stack do - # First condition is interpreted backwards ( if not ) - first_block = @blocks.first - unless first_block.evaluate(context) - return first_block.attachment.render_to_output_buffer(context, output) - end + # First condition is interpreted backwards ( if not ) + first_block = @blocks.first + unless first_block.evaluate(context) + return first_block.attachment.render_to_output_buffer(context, output) + end - # After the first condition unless works just like if - @blocks[1..-1].each do |block| - if block.evaluate(context) - return block.attachment.render_to_output_buffer(context, output) - end + # After the first condition unless works just like if + @blocks[1..-1].each do |block| + if block.evaluate(context) + return block.attachment.render_to_output_buffer(context, output) end end