Class: Sablon::Template
- Inherits:
-
Object
- Object
- Sablon::Template
- Defined in:
- lib/sablon/template.rb
Overview
Creates a template from an MS Word doc that can be easily manipulated
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Class Method Summary collapse
-
.get_processors(entry_name) ⇒ Object
Returns the processor classes with a pattern matching the entry name.
- .processors ⇒ Object
-
.register_processor(pattern, klass, replace_all: false) ⇒ Object
Adds a new processor to the processors hash.
Instance Method Summary collapse
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
-
#render_to_file(output_path, context, properties = {}) ⇒ Object
Same as
render_to_string
but writes the processed template tooutput_path
. -
#render_to_string(context, properties = {}) ⇒ Object
Process the template.
Constructor Details
#initialize(path) ⇒ Template
Returns a new instance of Template.
36 37 38 |
# File 'lib/sablon/template.rb', line 36 def initialize(path) @path = path end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
8 9 10 |
# File 'lib/sablon/template.rb', line 8 def document @document end |
Class Method Details
.get_processors(entry_name) ⇒ Object
Returns the processor classes with a pattern matching the entry name. If none match nil is returned.
26 27 28 29 |
# File 'lib/sablon/template.rb', line 26 def get_processors(entry_name) key = processors.keys.detect { |pattern| entry_name =~ pattern } processors[key] end |
.processors ⇒ Object
31 32 33 |
# File 'lib/sablon/template.rb', line 31 def processors @processors ||= Hash.new([]) end |
.register_processor(pattern, klass, replace_all: false) ⇒ Object
Adds a new processor to the processors hash. The pattern
is used to select which files the processor should handle. Multiple processors can be added for the same pattern.
14 15 16 17 18 19 20 21 22 |
# File 'lib/sablon/template.rb', line 14 def register_processor(pattern, klass, replace_all: false) processors[pattern] = [] if replace_all # if processors[pattern].empty? processors[pattern] = [klass] else processors[pattern] << klass end end |
Instance Method Details
#render_to_file(output_path, context, properties = {}) ⇒ Object
Same as render_to_string
but writes the processed template to output_path
.
41 42 43 44 45 |
# File 'lib/sablon/template.rb', line 41 def render_to_file(output_path, context, properties = {}) File.open(output_path, 'wb') do |f| f.write render_to_string(context, properties) end end |
#render_to_string(context, properties = {}) ⇒ Object
Process the template. The context
hash will be available in the template.
48 49 50 |
# File 'lib/sablon/template.rb', line 48 def render_to_string(context, properties = {}) render(context, properties).string end |