Sha256: fa94d5a2c3b900e60b8f3a29f9683f40510cdc5bd68c4565b0dc2ae1470e7e7c

Contents?: true

Size: 1.47 KB

Versions: 41

Compression:

Stored size: 1.47 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
    FilterParser = /(?:#{FilterSeparator}|(?:\s*(?!(?:#{FilterSeparator}))(?:#{QuotedFragment}|\S+)\s*)+)/
    attr_accessor :filters, :name

    def initialize(markup)
      @markup  = markup
      @name    = nil
      @filters = []
      if match = markup.match(/\s*(#{QuotedFragment})(.*)/)
        @name = match[1]
        if match[2].match(/#{FilterSeparator}\s*(.*)/)
          filters = Regexp.last_match(1).scan(FilterParser)
          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

41 entries across 41 versions & 5 rubygems

Version Path
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/liquid-2.2.2/lib/liquid/variable.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/liquid-2.2.2/lib/liquid/variable.rb
locomotive_liquid-2.2.3 lib/liquid/variable.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/liquid-2.2.2/lib/liquid/variable.rb
liquid_cms-0.3.2.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.2.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.1.1 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.1.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.1.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.13 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.0.10 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.0.9 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.12 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.11 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.10 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.0.8 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.9 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.0.7 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.3.0.6 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/variable.rb
liquid_cms-0.2.0.8 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/variable.rb