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.



229
230
231
# File 'lib/tenjin.rb', line 229

def _buf
  @_buf
end

#_engineObject

Returns the value of attribute _engine.



229
230
231
# File 'lib/tenjin.rb', line 229

def _engine
  @_engine
end

#_layoutObject

Returns the value of attribute _layout.



229
230
231
# File 'lib/tenjin.rb', line 229

def _layout
  @_layout
end

#_templateObject

Returns the value of attribute _template.



229
230
231
# File 'lib/tenjin.rb', line 229

def _template
  @_template
end

Instance Method Details

#_decode_params(s) ⇒ Object

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



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/tenjin.rb', line 338

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'



331
332
333
# File 'lib/tenjin.rb', line 331

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

#_p(arg) ⇒ Object

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



324
325
326
# File 'lib/tenjin.rb', line 324

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

#cache_with(cache_key, lifetime = nil) ⇒ Object

cache fragment data

ex.

kv_store = Tenjin::FileBaseStore.new("/var/tmp/myapp/dacache")
Tenjin::Engine.data_cache = kv_store
engine = Tenjin::Engine.new
    # or engine = Tenjin::Engine.new(:data_cache=>kv_store)
entries = proc { Entry.find(:all) }
html = engine.render("index.rbhtml", {:entries => entries})

index.rbhtml:

<html>
  <body>
    <?rb cache_with("entries/index", 5*60) do ?>
    <?rb   entries = @entries.call ?>
    <ul>
      <?rb for entry in entries ?>
      <li>${entry.title}</li>
      <?rb end ?>
    </ul>
    <?rb end ?>
  </body>
</html>


376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/tenjin.rb', line 376

def cache_with(cache_key, lifetime=nil)
  kv_store = self._engine.data_cache  or
    raise ArgumentError.new("data_cache object is not set for engine object.")
  data = kv_store.get(cache_key, self._template.timestamp)
  if data
    echo data
  else
    pos = self._buf.length
    yield
    data = self._buf[pos..-1]
    kv_store.set(cache_key, data, lifetime)
  end
  nil
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.



314
315
316
317
318
319
# File 'lib/tenjin.rb', line 314

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



246
247
248
# File 'lib/tenjin.rb', line 246

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

#escape(val) ⇒ Object

escape value. this method should be overrided in subclass.



232
233
234
# File 'lib/tenjin.rb', line 232

def escape(val)
  return val
end

#import(template_name, _append_to_buf = true) ⇒ Object

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



237
238
239
240
241
242
243
# File 'lib/tenjin.rb', line 237

def import(template_name, _append_to_buf=true)
  _buf = self._buf
  output = self._engine.render(template_name, context=self, layout=false)
  self._buf = _buf
  _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>


281
282
283
284
285
286
287
288
289
290
291
# File 'lib/tenjin.rb', line 281

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.



298
299
300
301
302
303
304
305
306
307
# File 'lib/tenjin.rb', line 298

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