Sha256: 823a0ab8cd74bdad5db7d3a2729b591d3333bd25866996d7146668e73970613f

Contents?: true

Size: 782 Bytes

Versions: 6

Compression:

Stored size: 782 Bytes

Contents

module Liquid

  # Assign sets a variable in your template.
  #
  #   {% assign foo = 'monkey' %}
  #
  # You can then use the variable later in the page.
  #
  #  {{ foo }}
  #
  class Assign < Tag
    Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/o

    def initialize(tag_name, markup, tokens)
      if markup =~ Syntax
        @to = $1
        @from = Variable.new($2)
      else
        raise SyntaxError.new("Syntax Error in 'assign' - Valid syntax: assign [var] = [source]")
      end

      super
    end

    def render(context)
      val = @from.render(context)
      context.scopes.last[@to] = val
      context.resource_limits[:assign_score_current] += (val.respond_to?(:length) ? val.length : 1)
      ''
    end

  end

  Template.register_tag('assign', Assign)
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
liquid-2.6.3 lib/liquid/tags/assign.rb
liquid-2.6.2 lib/liquid/tags/assign.rb
liquid-2.6.1 lib/liquid/tags/assign.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/liquid-2.6.0/lib/liquid/tags/assign.rb
liquid-2.6.0 lib/liquid/tags/assign.rb
liquid-2.6.0.rc1 lib/liquid/tags/assign.rb