Class: Tache

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

Direct Known Subclasses

Safe

Defined Under Namespace

Classes: Context, Parser, PartialCollection, Safe, SyntaxError, Template

Constant Summary collapse

ENTITIES =
{ '&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;' }
VERSION =
"0.4.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(source) ⇒ Object



36
37
38
# File 'lib/tache/tache.rb', line 36

def self.compile(source)
  new.compile(source)
end

.render(source, view = nil, partials = {}) ⇒ Object



40
41
42
43
44
# File 'lib/tache/tache.rb', line 40

def self.render(source, view = nil, partials = {})
  instance = compile(source)
  instance.partials = partials
  instance.render(view)
end

Instance Method Details

#compile(source, options = {}) ⇒ Object



4
5
6
7
# File 'lib/tache/tache.rb', line 4

def compile(source, options = {})
  @template = Template.new(source, options).compile
  self
end

#escape(string) ⇒ Object



31
32
33
34
# File 'lib/tache/tache.rb', line 31

def escape(string)
  # RubyMotion doesn't have CGI class, i.e. CGI.escapeHTML(string)
  string.gsub(/[&\"<>]/, ENTITIES)
end

#partial(name) ⇒ Object

TODO: test



27
28
29
# File 'lib/tache/tache.rb', line 27

def partial(name)
  nil
end

#partialsObject



17
18
19
# File 'lib/tache/tache.rb', line 17

def partials
  @partials ||= PartialCollection.new(self)
end

#partials=(hash) ⇒ Object



21
22
23
24
# File 'lib/tache/tache.rb', line 21

def partials=(hash)
  @partials = PartialCollection.new(self)
  hash.each { |key, value| @partials[key] = value }
end

#render(source_or_view = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/tache/tache.rb', line 9

def render(source_or_view = nil)
  case source_or_view
    when String then compile(source_or_view).render
    when nil then @template.render(context)
    else context.push(source_or_view) { |child| @template.render(child) }
  end
end