public
Rubygem
Description: Liquid markup language. Save, customer facing template language for flexible web apps.
Homepage: http://www.liquidmarkup.org
Clone URL: git://github.com/tobi/liquid.git
Click here to lend your support to: liquid and make a donation at www.pledgie.com !
liquid / lib / liquid / tags / assign.rb
100644 33 lines (27 sloc) 0.7 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Liquid
 
  # Assign sets a variable in your template.
  #
  # {% assign foo = 'monkey' %}
  #
  # You can then use the variable later in the page.
  #
  # {{ monkey }}
  #
  class Assign < Tag
    Syntax = /(#{VariableSignature}+)\s*=\s*(#{Expression}+)/
  
    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
  
    def render(context)
       context.scopes.last[@to.to_s] = context[@from]
       ''
    end
  
  end
  
  Template.register_tag('assign', Assign)
end