Class: Sbuilder::Ial::Action::Render::Renderer

Inherits:
Mustache
  • Object
show all
Includes:
MyLogger
Defined in:
lib/action/render/renderer.rb

Constant Summary collapse

PROGNAME =

progname for logger default class name

nil

Constants included from MyLogger

MyLogger::LOGFILE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MyLogger

#getLogger, #logfile

Methods inherited from Mustache

ctag, ctag=, otag, otag=

Constructor Details

#initialize(options = {}) ⇒ Renderer

Returns a new instance of Renderer.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/action/render/renderer.rb', line 60

def initialize( options={} ) 
  # exception raise if accessing unknown property
  self.raise_on_context_miss = true

  @options = options
  @logger = getLogger( nil, options )
  @logger.info "#{__method__}: starting options=#{options}"

  self.partials = {}
  
end

Instance Attribute Details

#loggerObject (readonly)



50
51
52
# File 'lib/action/render/renderer.rb', line 50

def logger
  @logger
end

#partialsObject



55
56
57
# File 'lib/action/render/renderer.rb', line 55

def partials
  @partials
end

#preferencesObject (readonly)



58
59
60
# File 'lib/action/render/renderer.rb', line 58

def preferences
  @preferences
end

Class Method Details

.start(options = {}) ⇒ Object



72
73
74
75
# File 'lib/action/render/renderer.rb', line 72

def self.start( options = {} )
  renderer = Renderer.new( options )
  renderer
end

Instance Method Details

#EVAL(key) ⇒ Object

string lookup key in current context

Parameters:

  • key (String, '.')

    to render, ‘.’ means current context,

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/action/render/renderer.rb', line 101

def EVAL( key )
  
  logger.info "#{__method__}: key='#{key}', context.current=#{context.current}" if logger.debug?
  
  # find generate sexp( :sym, data ) to render 
  if key == '.'
    sexp = context.current
  else
    #  to evaluate 
    hash = context.current.is_a?( Array ) ? context.current[1] : context.current
    sexp = hash[key] || hash[key.to_sym]
    raise MustacheException, "No such key #{key}:#{key.class} found in #{hash}"  if sexp.nil?      
  end
  
  sexp_type = sexp[0]
  data = sexp[1]
  logger.debug "#{__method__}: key=#{key}, sexp_type=#{sexp_type}" if logger.debug?
  

  # find correct template 
  template = partials[sexp_type]
  raise MustacheException,
        <<-EOS if (template.nil?)
        Unknown partial for sexp_type '#{sexp_type}'

        Known partials: #{partials.keys.join(',')}"  
        Conxt = #{context.current}
        EOS

  logger.debug "#{__method__}: template=#{template}, data=#{data}" if logger.debug?    
  return render( template, data )

end

#setPreferences(preferences) ⇒ Object



77
78
79
80
# File 'lib/action/render/renderer.rb', line 77

def setPreferences( preferences )
  @preferences = preferences
  @logger.info "#{__method__}:  preferences=#{preferences}"
end

#to_str(template, data = nil) ⇒ Object

Use ‘to_str’ instead of ‘render’ to use non-default otag/ctag’s



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/action/render/renderer.rb', line 83

def to_str( template, data=nil )
  Mustache.otag = '{|'
  Mustache.ctag = '|}'    

  begin 
    ret = render( template, data )
  ensure
    # restore default otag/ctag
    Mustache.otag = '{{'
    Mustache.ctag = '}}'
  end
  ret
end