Sha256: 1ef6e13ac8a21a6fcb01c27c4d3106fc62f2ee294a396cd37dfada37dd175be5

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module Liquid

  # Holds variables. Variables are only loaded "just in time"
  # and are not evaluated as part of the render stage
  #
  #   {{ monkey }}
  #   {{ user.name }}
  #
  # Variables can be combined with filters:
  #
  #   {{ user | link }}
  #
  class Variable
    attr_accessor :filters, :name

    def initialize(markup)
      @markup  = markup
      @name    = nil
      @filters = []
      if match = markup.match(/\s*(#{QuotedFragment})/)
        @name = match[1]
        if markup.match(/#{FilterSeparator}\s*(.*)/)
          filters = Regexp.last_match(1).split(/#{FilterSeparator}/)
          filters.each do |f|
            if matches = f.match(/\s*(\w+)/)
              filtername = matches[1]
              filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten
              @filters << [filtername.to_sym, filterargs]
            end
          end
        end
      end
    end

    def render(context)
      return '' if @name.nil?
      @filters.inject(context[@name]) do |output, filter|
        filterargs = filter[1].to_a.collect do |a|
         context[a]
        end
        begin
          output = context.invoke(filter[0], output, *filterargs)
        rescue FilterNotFound
          raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found."
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 4 rubygems

Version Path
jeremyf-liquid-2.0.2 lib/liquid/variable.rb
qoobaa-liquid-2.0.1 lib/liquid/variable.rb
tobi-liquid-2.0.1 lib/liquid/variable.rb
drnic-liquid-2.1.0 lib/liquid/variable.rb