Module: Clamsy::Tenjin::ContextHelper

Included in:
BaseContext
Defined in:
lib/clamsy/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.



112
113
114
# File 'lib/clamsy/tenjin.rb', line 112

def _buf
  @_buf
end

#_engineObject

Returns the value of attribute _engine.



112
113
114
# File 'lib/clamsy/tenjin.rb', line 112

def _engine
  @_engine
end

#_layoutObject

Returns the value of attribute _layout.



112
113
114
# File 'lib/clamsy/tenjin.rb', line 112

def _layout
  @_layout
end

Instance Method Details

#_decode_params(s) ⇒ Object

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



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/clamsy/tenjin.rb', line 220

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'



213
214
215
# File 'lib/clamsy/tenjin.rb', line 213

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

#_p(arg) ⇒ Object

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



206
207
208
# File 'lib/clamsy/tenjin.rb', line 206

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.



196
197
198
199
200
201
# File 'lib/clamsy/tenjin.rb', line 196

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’.



128
129
130
# File 'lib/clamsy/tenjin.rb', line 128

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

#escape(val) ⇒ Object

escape value. this method should be overrided in subclass.



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

def escape(val)
  return val
end

#import(template_name, _append_to_buf = true) ⇒ Object

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



120
121
122
123
124
125
# File 'lib/clamsy/tenjin.rb', line 120

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>{? start_capture(:title) do ?}Document Title{? end ?}</h1>
  {? start_capture(:content) ?}
  <ul>
   {? for item in list do ?}
    <li>${item}</li>
   {? end ?}
  </ul>
  {? 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">
   {? echo(@content) ?}
  </div>
 </body>
</html>


163
164
165
166
167
168
169
170
171
172
173
# File 'lib/clamsy/tenjin.rb', line 163

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.



180
181
182
183
184
185
186
187
188
189
# File 'lib/clamsy/tenjin.rb', line 180

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