Sha256: b53a04cfab87ef9ff75db20319124ecc29e702214a4fb4d0ec5a65df1cda447d

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require_relative 'if'

module Liquid
  # @liquid_public_docs
  # @liquid_type tag
  # @liquid_category conditional
  # @liquid_name unless
  # @liquid_summary
  #   Renders an expression unless a specific condition is `true`.
  # @liquid_description
  #   > Tip:
  #   > Similar to the [`if` tag](/docs/api/liquid/tags/if), you can use `elsif` to add more conditions to an `unless` tag.
  # @liquid_syntax
  #   {% unless condition %}
  #     expression
  #   {% endunless %}
  # @liquid_syntax_keyword condition The condition to evaluate.
  # @liquid_syntax_keyword expression The expression to render unless the condition is met.
  class Unless < If
    def render_to_output_buffer(context, output)
      # First condition is interpreted backwards ( if not )
      first_block = @blocks.first
      result = Liquid::Utils.to_liquid_value(
        first_block.evaluate(context),
      )

      unless result
        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|
        result = Liquid::Utils.to_liquid_value(
          block.evaluate(context),
        )

        if result
          return block.attachment.render_to_output_buffer(context, output)
        end
      end

      output
    end
  end

  Template.register_tag('unless', Unless)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
liquid-5.5.1 lib/liquid/tags/unless.rb
liquid-5.5.0 lib/liquid/tags/unless.rb