Class: Rtpl

Inherits:
Object
  • Object
show all
Defined in:
lib/rtpl.rb,
lib/rtpl/version.rb

Defined Under Namespace

Classes: Parser, Transform

Constant Summary collapse

VERSION =
'0.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Rtpl

Returns a new instance of Rtpl.



11
12
13
14
15
16
17
18
# File 'lib/rtpl.rb', line 11

def initialize(template)
  @ast                   = Rtpl::Parser.new.parse(template)
  @vars                  = {}
  @parsed_blocks         = {}
  @parsed_blocks.default = ""
  @transform             = Rtpl::Transform.new
  @sub_blocks            = false
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



9
10
11
# File 'lib/rtpl.rb', line 9

def ast
  @ast
end

#parsed_blocksObject (readonly)

Returns the value of attribute parsed_blocks.



9
10
11
# File 'lib/rtpl.rb', line 9

def parsed_blocks
  @parsed_blocks
end

#varsObject (readonly)

Returns the value of attribute vars.



9
10
11
# File 'lib/rtpl.rb', line 9

def vars
  @vars
end

Instance Method Details

#assign(key, value) ⇒ Object



20
21
22
# File 'lib/rtpl.rb', line 20

def assign(key, value)
  @vars[key] = value
end

#out(key = "main") ⇒ Object



37
38
39
# File 'lib/rtpl.rb', line 37

def out(key = "main")
  @parsed_blocks[key].to_s
end

#parse(block_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rtpl.rb', line 24

def parse(block_name)
  if block_name =~ /\./
    @sub_blocks = true
    @parsed_blocks[block_name] +=
      nested_output(@transform.apply(sub_block(block_name), :vars => @vars),
                    block_name)
  else
    @parsed_blocks[block_name] +=
      block_output(@transform.apply(@ast, :vars => @vars).
                   detect{|b| b[:open][:name] == block_name })
  end
end