Class: Tenjin::Template
- Inherits:
-
Object
- Object
- Tenjin::Template
- Defined in:
- lib/tenjin.rb
Overview
template class
ex. file ‘example.rbhtml’
<html>
<body>
<h1>${@title}</h1>
<ul>
<?rb i = 0 ?>
<?rb for item in @items ?>
<?rb i += 1 ?>
<li>#{i} : ${item}</li>
<?rb 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
ArrayBufferTemplate, ErubisTemplate, Preprocessor, SafeTemplate
Constant Summary collapse
- ESCAPE_FUNCTION =
or ‘Eruby::Helper.escape’
'escape'
- TRACE =
false
Instance Attribute Summary collapse
-
#_last_checked_at ⇒ Object
Returns the value of attribute _last_checked_at.
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
-
#convert(input, filename = nil) ⇒ Object
convert string into ruby code.
-
#convert_file(filename) ⇒ Object
convert file into ruby code.
-
#finish_buf_expr ⇒ Object
:nodoc:.
-
#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)
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/tenjin.rb', line 509 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 ? finish_buf_expr() : [:postamble] @input = [:input] @trace = [:trace] || TRACE @args = nil # or array of argument names if @input convert(@input, filename) elsif filename convert_file(filename) end end |
Instance Attribute Details
#_last_checked_at ⇒ Object
Returns the value of attribute _last_checked_at.
530 531 532 |
# File 'lib/tenjin.rb', line 530 def _last_checked_at @_last_checked_at end |
#args ⇒ Object
Returns the value of attribute args.
528 529 530 |
# File 'lib/tenjin.rb', line 528 def args @args end |
#escapefunc ⇒ Object
Returns the value of attribute escapefunc.
527 528 529 |
# File 'lib/tenjin.rb', line 527 def escapefunc @escapefunc end |
#filename ⇒ Object
Returns the value of attribute filename.
527 528 529 |
# File 'lib/tenjin.rb', line 527 def filename @filename end |
#initbuf ⇒ Object
Returns the value of attribute initbuf.
527 528 529 |
# File 'lib/tenjin.rb', line 527 def initbuf @initbuf end |
#newline ⇒ Object
Returns the value of attribute newline.
527 528 529 |
# File 'lib/tenjin.rb', line 527 def newline @newline end |
#script ⇒ Object
,:bytecode
529 530 531 |
# File 'lib/tenjin.rb', line 529 def script @script end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
528 529 530 |
# File 'lib/tenjin.rb', line 528 def @timestamp end |
Class Method Details
.TRACE=(boolean) ⇒ Object
496 497 498 499 |
# File 'lib/tenjin.rb', line 496 def self.TRACE=(boolean) remove_const :TRACE const_set :TRACE, boolean end |
Instance Method Details
#convert(input, filename = nil) ⇒ Object
convert string into ruby code
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/tenjin.rb', line 538 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
533 534 535 |
# File 'lib/tenjin.rb', line 533 def convert_file(filename) return convert(File.read(filename), filename) end |
#finish_buf_expr ⇒ Object
:nodoc:
783 784 785 |
# File 'lib/tenjin.rb', line 783 def finish_buf_expr() # :nodoc: return "_buf.to_s" end |
#init_buf_expr ⇒ Object
:nodoc:
779 780 781 |
# File 'lib/tenjin.rb', line 779 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.
789 790 791 792 793 794 795 796 797 798 799 800 801 |
# File 'lib/tenjin.rb', line 789 def render(_context=Context.new) _context = Context.new(_context) if _context.is_a?(Hash) @proc ||= _render() if @trace s = "" s << "<!-- ***** begin: #{@filename} ***** -->\n" s << _context.instance_eval(&@proc) s << "<!-- ***** end: #{@filename} ***** -->\n" return s else return _context.instance_eval(&@proc) end end |