Class: AwsMust::Template

Inherits:
Mustache
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/aws-must/template.rb

Constant Summary collapse

PROGNAME =

progname for logger

"template"

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger

Constructor Details

#initialize(options = {}) ⇒ Template


Constructor



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws-must/template.rb', line 22

def initialize( options={} )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} created" )
  @logger.debug( "#{__method__}, options='#{options}" )

  @template_extension = "mustache"# type part in filename

  # for mustache templates
  if options[:template_paths] && options[:template_paths].any? then
    @template_paths = options[:template_paths]
  else
    @template_paths = ( options[:template_path].respond_to?(:each) ? options[:template_path] : [ options[:template_path]  ] )
  end

end

Instance Attribute Details

#partials=(value) ⇒ Object (writeonly)

instance



16
17
18
# File 'lib/aws-must/template.rb', line 16

def partials=(value)
  @partials = value
end

#templates=(value) ⇒ Object (writeonly)

f: template-name –> template string



17
18
19
# File 'lib/aws-must/template.rb', line 17

def templates=(value)
  @templates = value
end

Instance Method Details

#get_partial(name) ⇒ Object

cache @partials - for easier extension



63
64
65
66
67
68
69
70
# File 'lib/aws-must/template.rb', line 63

def get_partial( name ) 
  @logger.debug( "#{__method__} name=#{name}" )
  return @partials[name] if @partials && @partials[name]

  partial_file = get_template_filepath( name )
  @logger.info( "#{__method__} read partial_file=#{partial_file}" )
  File.read( partial_file )
end

#get_template(name) ⇒ Object

hide @templates - for easier extension



73
74
75
76
77
78
79
# File 'lib/aws-must/template.rb', line 73

def get_template( name ) 

  template_file = get_template_filepath( name )
  @logger.info( "#{__method__} read template_file=#{template_file}" )
  File.read( template_file )

end

#partial(name) ⇒ Object

method used by mustache framework - delegate to ‘get_partial’



57
58
59
60
# File 'lib/aws-must/template.rb', line 57

def partial(name)
  @logger.debug( "#{__method__} name=#{name}" )
  get_partial( name )
end

#to_str(template_name = nil, data = nil) ⇒ Object


Services



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aws-must/template.rb', line 41

def to_str( template_name=nil, data=nil )
  @logger.debug( "#{__method__}: template_name=#{template_name}, data=#{data}" )
  if template_name 
    template = get_template( template_name )
    render( template, data )
  elsif data
    render( data )
  else
    puts @templates
  end
end