Class: Clamsy::Tenjin::Template
- Inherits:
-
Object
- Object
- Clamsy::Tenjin::Template
- Defined in:
- lib/clamsy/tenjin.rb
Overview
template class
ex. file ‘example.rbhtml’
<html>
<body>
<h1>${@title}</h1>
<ul>
{? i = 0 ?}
{? for item in @items ?}
{? i += 1 ?}
<li>#{i} : ${item}</li>
{? end ?}
</ul>
</body>
</html>
ex. convertion
require 'tenjin'
template = Tenjin::Template.new('example.rbhtml')
print template.script
## or
# template = Tenjin::Template.new()
# print template.convert_file('example.rbhtml')
## or
# template = Tenjin::Template.new()
# fname = 'example.rbhtml'
# print template.convert(File.read(fname), fname) # filename is optional
ex. evaluation
context = {:title=>'Tenjin Example', :items=>['foo', 'bar', 'baz'] }
output = template.render(context)
## or
# context = Tenjin::Context(:title=>'Tenjin Example', :items=>['foo','bar','baz'])
# output = template.render(context)
## or
# output = template.render(:title=>'Tenjin Example', :items=>['foo','bar','baz'])
print output
Direct Known Subclasses
Constant Summary collapse
- ESCAPE_FUNCTION =
or ‘Eruby::Helper.escape’
'escape'
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#escapefunc ⇒ Object
Returns the value of attribute escapefunc.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#initbuf ⇒ Object
Returns the value of attribute initbuf.
-
#newline ⇒ Object
Returns the value of attribute newline.
-
#script ⇒ Object
,:bytecode.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#convert(input, filename = nil) ⇒ Object
convert string into ruby code.
-
#convert_file(filename) ⇒ Object
convert file into ruby code.
-
#init_buf_expr ⇒ Object
:nodoc:.
-
#initialize(filename = nil, options = {}) ⇒ Template
constructor
initializer of Template class.
-
#render(_context = Context.new) ⇒ Object
evaluate converted ruby code and return it.
Constructor Details
#initialize(filename = nil, options = {}) ⇒ Template
initializer of Template class.
options:
- :escapefunc
-
function name to escape value (default ‘escape’)
- :preamble
-
preamble such as “_buf = ”” (default nil)
- :postamble
-
postamble such as “_buf.to_s” (default nil)
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/clamsy/tenjin.rb', line 344 def initialize(filename=nil, ={}) if filename.is_a?(Hash) = filename filename = nil end @filename = filename @escapefunc = [:escapefunc] || ESCAPE_FUNCTION @preamble = [:preamble] == true ? "_buf = #{init_buf_expr()}; " : [:preamble] @postamble = [:postamble] == true ? "_buf.to_s" : [:postamble] @args = nil # or array of argument names convert_file(filename) if filename end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
357 358 359 |
# File 'lib/clamsy/tenjin.rb', line 357 def args @args end |
#escapefunc ⇒ Object
Returns the value of attribute escapefunc.
356 357 358 |
# File 'lib/clamsy/tenjin.rb', line 356 def escapefunc @escapefunc end |
#filename ⇒ Object
Returns the value of attribute filename.
356 357 358 |
# File 'lib/clamsy/tenjin.rb', line 356 def filename @filename end |
#initbuf ⇒ Object
Returns the value of attribute initbuf.
356 357 358 |
# File 'lib/clamsy/tenjin.rb', line 356 def initbuf @initbuf end |
#newline ⇒ Object
Returns the value of attribute newline.
356 357 358 |
# File 'lib/clamsy/tenjin.rb', line 356 def newline @newline end |
#script ⇒ Object
,:bytecode
358 359 360 |
# File 'lib/clamsy/tenjin.rb', line 358 def script @script end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
357 358 359 |
# File 'lib/clamsy/tenjin.rb', line 357 def @timestamp end |
Instance Method Details
#convert(input, filename = nil) ⇒ Object
convert string into ruby code
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/clamsy/tenjin.rb', line 366 def convert(input, filename=nil) @input = input @filename = filename @proc = nil pos = input.index(?\n) if pos && input[pos-1] == ?\r @newline = "\r\n" @newlinestr = '\\r\\n' else @newline = "\n" @newlinestr = '\\n' end before_convert() parse_stmts(input) after_convert() return @script end |
#convert_file(filename) ⇒ Object
convert file into ruby code
361 362 363 |
# File 'lib/clamsy/tenjin.rb', line 361 def convert_file(filename) return convert(File.read(filename), filename) end |
#init_buf_expr ⇒ Object
:nodoc:
623 624 625 |
# File 'lib/clamsy/tenjin.rb', line 623 def init_buf_expr() # :nodoc: return "''" end |
#render(_context = Context.new) ⇒ Object
evaluate converted ruby code and return it. argument ‘_context’ should be a Hash object or Context object.
629 630 631 632 633 |
# File 'lib/clamsy/tenjin.rb', line 629 def render(_context=Context.new) _context = Context.new(_context) if _context.is_a?(Hash) @proc ||= _render() return _context.instance_eval(&@proc) end |