Sha256: 73a9807c265db5c9d8bce7aa1159875f09d188dd3b2a644d5d01f0e7d6856620

Contents?: true

Size: 794 Bytes

Versions: 4

Compression:

Stored size: 794 Bytes

Contents

require File.dirname(__FILE__) + '/if'

module Liquid
  # Unless is a conditional just like 'if' but works on the inverse logic.
  #
  #   {% unless x < 0 %} x is greater than zero {% endunless %}
  #
  class Unless < If
    def render(context)
      context.stack do

        # First condition is interpreted backwards ( if not )
        first_block = @blocks.first
        unless first_block.evaluate(context)
          return first_block.attachment.render(context)
        end

        # After the first condition unless works just like if
        @blocks[1..-1].each do |block|
          if block.evaluate(context)
            return block.attachment.render(context)
          end
        end

        ''.freeze
      end
    end
  end

  Template.register_tag('unless'.freeze, Unless)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotivecms-liquid-4.0.0 lib/liquid/tags/unless.rb
locomotivecms-liquid-4.0.0.alpha2 lib/liquid/tags/unless.rb
locomotivecms-liquid-4.0.0.alpha1 lib/liquid/tags/unless.rb
locomotivecms-liquid-4.0.0.alpha lib/liquid/tags/unless.rb