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*/o

Instance Attribute Summary

Attributes inherited from Tag

#context, #nodelist

Instance Method Summary collapse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ 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, context)
  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

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] = @from.render(context)
   ''
end