Class: Sbuilder::Template
- Inherits:
-
TemplateRoot
- Object
- Mustache
- TemplateRoot
- Sbuilder::Template
- Includes:
- Utils::MyLogger
- Defined in:
- lib/sbuilder/mustache/template.rb
Constant Summary collapse
- PROGNAME =
nil
Constants included from Utils::MyLogger
Instance Attribute Summary collapse
-
#partials ⇒ Object
writeonly
instance.
Attributes inherited from TemplateRoot
Called from 'partial' -method collapse
-
#get_partial(name) ⇒ Object
Intercept chain to read template file, and cache @partials.
private collapse
-
#resolve_partial_name(name) ⇒ String
Return name of partial, which normally is simply ‘name’ parameer, but which can be found form ‘@data’ using indirect address following starting exclamation mark.
Instance Method Summary collapse
-
#data ⇒ Object
using lambda function.
-
#initialize(reader, options = {}) ⇒ Template
constructor
—————————————————————— Constructor.
- #render_str(template, data) ⇒ Object
- #setData(data = {}) ⇒ Object
- #to_str(template_name, data) ⇒ Object
Methods included from Utils::MyLogger
Methods inherited from TemplateRoot
Constructor Details
#initialize(reader, options = {}) ⇒ Template
Constructor
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sbuilder/mustache/template.rb', line 18 def initialize( reader, ={} ) super( reader, ) @logger = getLogger( PROGNAME, ) @logger.info( "#{__method__} created" ) @logger.debug( "#{__method__}, options='#{}" ) # init partial cache @partials = {} # inidirection data init to empty hash setData( {} ) end |
Instance Attribute Details
#partials=(value) ⇒ Object (writeonly)
instance
14 15 16 |
# File 'lib/sbuilder/mustache/template.rb', line 14 def partials=(value) @partials = value end |
Instance Method Details
#data ⇒ Object
using lambda function
111 112 113 |
# File 'lib/sbuilder/mustache/template.rb', line 111 def data return @data end |
#get_partial(name) ⇒ Object
Intercept chain to read template file, and cache @partials.
Delegates actual read operation to ‘get_template’ in parent class reader.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sbuilder/mustache/template.rb', line 127 def get_partial( name ) @logger.debug( "#{__method__} name=#{name}" ) # return indirection for 'name[2..-1]) ' if name[0..1] == '!!' name = resolve_partial_name(name) return @partials[name] if @partials[name] @logger.info( "#{__method__} read partial_file=#{name}" ) @partials[name] = get_template( name ) return @partials[name] end |
#render_str(template, data) ⇒ Object
98 99 100 |
# File 'lib/sbuilder/mustache/template.rb', line 98 def render_str( template, data ) render( template, data ) end |
#resolve_partial_name(name) ⇒ String
Return name of partial, which normally is simply ‘name’ parameer, but which can be found form ‘@data’ using indirect address following starting exclamation mark.
minus prefixed exclamation
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/sbuilder/mustache/template.rb', line 151 def resolve_partial_name( name ) return name unless name[0..0] == '!' # remove !, split, and use injected traves to access data ret = @data.traverse( *(name[1..-1].split( "." )) ) if ret.nil? raise <<-EOS Could not resolve #{name} in template data #{@data.to_yaml} EOS end @logger.info( "#{__method__} name=#{name} --> #{ret}" ) ret end |
#setData(data = {}) ⇒ Object
105 106 107 |
# File 'lib/sbuilder/mustache/template.rb', line 105 def setData( data={} ) @data = data end |
#to_str(template_name, data) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sbuilder/mustache/template.rb', line 62 def to_str( template_name, data ) @logger.info( "#{__method__}: template_name=#{template_name}" ) @logger.debug( "#{__method__}: template_name=#{template_name}" ) if @logger.debug? # @logger.debug( "#{__method__}: nodes=#{data.nodes}" ) # parent class keeps tract of data, and get_template may need it # for indirection setData( data ) if is_extension_point( template_name ) then # # Use a dynamically created string to output extension points # template = extension_template( template_name ) @logger.debug "#{__method__} template: is extension #{template_name} -> #{template}" if @logger.debug? else # reader in parent class returns template to resolve as a # string. # # Normally reads template from file to which 'template_name' # resolves. It may be also an empty string (if reader has # decided that 'template_name' should not be shown. template = get_template( template_name ) @logger.debug "#{__method__} template: is not extension #{template_name} -> #{template}" if @logger.debug? # render( template, @data ) end # render( template, @data ) render_str( template, data ) end |