Class: Plasma::Template::PlasmaTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/plasma/template/plasma_template.rb

Constant Summary collapse

@@separator =
/##*[^#]+##*/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ PlasmaTemplate

Returns a new instance of PlasmaTemplate.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/plasma/template/plasma_template.rb', line 12

def initialize(template)
  @template = template
  @plasma = Plasma::Interpreter::PlasmaInterpreter.new

  @plain = []
  @plasmic = []

  cursor = @template

  loop do 
    pre = cursor.match(@@separator)
    
    if pre.nil?
      @plain << cursor
      break
    else
      @plain << pre.pre_match
      code = pre.values_at(0)[0]
      post = pre.post_match.match(@@separator)

      if post.nil?
        @plain << pre.post_match
        break
      else
        code += post.pre_match + post.values_at(0)[0]
        cursor = post.post_match

        @plasmic << @plasma.parse(code)
      end
    end
  end
end

Instance Attribute Details

#plasmaObject

Returns the value of attribute plasma.



4
5
6
# File 'lib/plasma/template/plasma_template.rb', line 4

def plasma
  @plasma
end

Class Method Details

.parse(template) ⇒ Object



8
9
10
# File 'lib/plasma/template/plasma_template.rb', line 8

def self.parse(template)
  return PlasmaTemplate.new(template)
end

Instance Method Details

#render(rel = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/plasma/template/plasma_template.rb', line 45

def render(rel={})
  @plasma.env.merge!(rel)
  evaluated = @plasmic.map{|p| @plasma.evaluate(p)}

  rendered = ''
  @plain.slice(0...@plain.length-1).each_with_index do |text, index|
    portion = text + evaluated[index].to_s
    rendered += portion
  end

  return rendered + @plain.last
end