Class: Jam::Template

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

Overview

Jam Templates are data-driven templates. They take valid XML/XHTML documents and expand them based on the data structures provided.

– TODO: Should rendered output have document headers like <?xml or a doctype for HTML. ++

Constant Summary collapse

DEFAULT_ADAPTER =
'Nokogiri'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, adapter = DEFAULT_ADAPTER, *options) ⇒ Template

New template from source string.

  • source can be either a String or an IO object.

  • adapter is either :nokogiri, :libxml or :rexml.

  • options are per-adapter and passed through to the underlying adapter’s parser



36
37
38
39
40
# File 'lib/jam/template.rb', line 36

def initialize(source, adapter=DEFAULT_ADAPTER, *options)
  #@engine  = Engine.new(adapter)
  @engine = Jam.const_get(adapter.to_s).new(*options)
  @source = source
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



16
17
18
# File 'lib/jam/template.rb', line 16

def engine
  @engine
end

Class Method Details

.load(file, adapter = DEFAULT_ADAPTER, *options) ⇒ Object

New template from source file.

  • file is the file location of a source.

  • adapter is either :nokogiri, :libxml or :rexml.

  • options are per-adapter and passed through to the underlying adapter’s parser



26
27
28
# File 'lib/jam/template.rb', line 26

def self.load(file, adapter=DEFAULT_ADAPTER, *options)
  new(File.read(file), adapter, *options)
end

Instance Method Details

#documentObject



43
44
45
# File 'lib/jam/template.rb', line 43

def document
  @document ||= engine.document(@source)
end

#render(data) ⇒ Object Also known as: expand

TODO: Should we render as doc, this might add a header, or render as root where it does not.



50
51
52
# File 'lib/jam/template.rb', line 50

def render(data)
  engine.interpolate(document, data).root.to_s
end

#to_sObject



57
58
59
# File 'lib/jam/template.rb', line 57

def to_s
  document.to_s
end