Sha256: ef72e14f8f589e747d16eda7ebcc3082aa6a6c3b9fe2071c8fcc5cbf2f1fb234

Contents?: true

Size: 880 Bytes

Versions: 1

Compression:

Stored size: 880 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*/om

    def initialize(tag_name, markup, options)
      super
      if markup =~ Syntax
        @to = $1
        @from = Variable.new($2, options)
      else
        raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze)
      end
    end

    def render(context)
      val = @from.render(context)
      context.scopes.last[@to] = val

      inc = val.instance_of?(String) || val.instance_of?(Array) || val.instance_of?(Hash) ? val.length : 1
      context.resource_limits.assign_score += inc

      ''.freeze
    end

    def blank?
      true
    end
  end

  Template.register_tag('assign'.freeze, Assign)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liquid-4.0.0.rc1 lib/liquid/tags/assign.rb