Class: Liquid::Assign

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/tags/assign.rb

Overview

Assign sets a variable in your template.

{% assign foo = 'monkey' %}

You can then use the variable later in the page.

{{ foo }}

Constant Summary collapse

Syntax =
/(#{VariableSignature}+)\s*=\s*(.*)\s*/om

Instance Attribute Summary

Attributes inherited from Tag

#line_number, #nodelist, #options, #warnings

Instance Method Summary collapse

Methods inherited from Tag

#name, #parse, parse, #raw

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ Assign

Returns a new instance of Assign.



14
15
16
17
18
19
20
21
22
23
# File 'lib/liquid/tags/assign.rb', line 14

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

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/liquid/tags/assign.rb', line 32

def blank?
  true
end

#render(context) ⇒ Object



25
26
27
28
29
30
# File 'lib/liquid/tags/assign.rb', line 25

def render(context)
  val = @from.render(context)
  context.scopes.last[@to] = val
  context.increment_used_resources(:assign_score_current, val)
  ''.freeze
end