Class: Jam::Template
- Inherits:
-
Object
- Object
- Jam::Template
- 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
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
Class Method Summary collapse
-
.load(file, adapter = DEFAULT_ADAPTER, *options) ⇒ Object
New template from source file.
Instance Method Summary collapse
- #document ⇒ Object
-
#initialize(source, adapter = DEFAULT_ADAPTER, *options) ⇒ Template
constructor
New template from source string.
-
#render(data) ⇒ Object
(also: #expand)
TODO: Should we render as doc, this might add a header, or render as root where it does not.
- #to_s ⇒ Object
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, *) #@engine = Engine.new(adapter) @engine = Jam.const_get(adapter.to_s).new(*) @source = source end |
Instance Attribute Details
#engine ⇒ Object (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, *) new(File.read(file), adapter, *) end |
Instance Method Details
#document ⇒ Object
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_s ⇒ Object
57 58 59 |
# File 'lib/jam/template.rb', line 57 def to_s document.to_s end |