Module: Tenjin::ContextHelper

Included in:
BaseContext
Defined in:
lib/tenjin.rb

Overview

helper module for BaseContext class

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_bufObject

Returns the value of attribute _buf.



105
106
107
# File 'lib/tenjin.rb', line 105

def _buf
  @_buf
end

#_engineObject

Returns the value of attribute _engine.



105
106
107
# File 'lib/tenjin.rb', line 105

def _engine
  @_engine
end

#_layoutObject

Returns the value of attribute _layout.



105
106
107
# File 'lib/tenjin.rb', line 105

def _layout
  @_layout
end

Instance Method Details

#_decode_params(s) ⇒ Object

decode <‘#…#`> and <`$…$`> into #… and $…



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tenjin.rb', line 208

def _decode_params(s)
  require 'cgi'
  return s unless s.is_a?(String)
  s = s.dup
  s.gsub!(/%3C%60%23(.*?)%23%60%3E/im) { "\#\{#{CGI::unescape($1)}\}" }
  s.gsub!(/%3C%60%24(.*?)%24%60%3E/im) { "\$\{#{CGI::unescape($1)}\}" }
  s.gsub!(/&lt;`\#(.*?)\#`&gt;/m) { "\#\{#{CGI::unescapeHTML($1)}\}" }
  s.gsub!(/&lt;`\$(.*?)\$`&gt;/m) { "\$\{#{CGI::unescapeHTML($1)}\}" }
  s.gsub!(/<`\#(.*?)\#`>/m, '#{\1}')
  s.gsub!(/<`\$(.*?)\$`>/m, '${\1}')
  return s
end

#_p(arg) ⇒ Object

ex. _p(“item”) => #'name'



194
195
196
# File 'lib/tenjin.rb', line 194

def _p(arg)
  return "<`\##{arg}\#`>"    # decoded into #{...} by preprocessor
end

#_P(arg) ⇒ Object

ex. _P(“item”) => $'name'



201
202
203
# File 'lib/tenjin.rb', line 201

def _P(arg)
  return "<`$#{arg}$`>"    # decoded into ${...} by preprocessor
end

#captured_as(name) ⇒ Object

if captured string is found then add it to _buf and return true, else return false. this is a helper method for layout template.



184
185
186
187
188
189
# File 'lib/tenjin.rb', line 184

def captured_as(name)
  str = self.instance_variable_get("@#{name}")
  return false unless str
  @_buf << str
  return true
end

#echo(value) ⇒ Object

add value into _buf. this is equivarent to ‘#value’.



116
117
118
# File 'lib/tenjin.rb', line 116

def echo(value)
  self._buf << value
end

#import(template_name, _append_to_buf = true) ⇒ Object

include template. ‘template_name’ can be filename or short name.



108
109
110
111
112
113
# File 'lib/tenjin.rb', line 108

def import(template_name, _append_to_buf=true)
  _buf = self._buf
  output = self._engine.render(template_name, context=self, layout=false)
  _buf << output if _append_to_buf
  return output
end

#start_capture(varname = nil) ⇒ Object

start capturing. returns captured string if block given, else return nil. if block is not given, calling stop_capture() is required.

ex. list.rbhtml

<html><body>
  <h1><?rb start_capture(:title) do ?>Document Title<?rb end ?></h1>
  <?rb start_capture(:content) ?>
  <ul>
   <?rb for item in list do ?>
    <li>${item}</li>
   <?rb end ?>
  </ul>
  <?rb stop_capture() ?>
</body></html>

ex. layout.rbhtml

<?xml version="1.0" ?>
<html xml:lang="en">
 <head>
  <title>${@title}</title>
 </head>
 <body>
  <h1>${@title}</h1>
  <div id="content">
   <?rb echo(@content) ?>
  </div>
 </body>
</html>


151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tenjin.rb', line 151

def start_capture(varname=nil)
  @_capture_varname = varname
  @_start_position = self._buf.length
  if block_given?
    yield
    output = stop_capture()
    return output
  else
    return nil
  end
end

#stop_capture(store_to_context = true) ⇒ Object

stop capturing. returns captured string. see start_capture()‘s document.



168
169
170
171
172
173
174
175
176
177
# File 'lib/tenjin.rb', line 168

def stop_capture(store_to_context=true)
  output = self._buf[@_start_position..-1]
  self._buf[@_start_position..-1] = ''
  @_start_position = nil
  if @_capture_varname
    self.instance_variable_set("@#{@_capture_varname}", output) if store_to_context
    @_capture_varname = nil
  end
  return output
end