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*(#{QuotedFragment}+)/

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ 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, tokens)          
  if markup =~ Syntax
    @to = $1
    @from = $2
  else
    raise SyntaxError.new("Syntax Error in 'assign' - Valid syntax: assign [var] = [source]")
  end
  
  super      
end

Instance Method Details

#render(context) ⇒ Object



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

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