Class: Tenjin::Template

Inherits:
Object
  • Object
show all
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

Constant Summary collapse

ESCAPE_FUNCTION =

or ‘Eruby::Helper.escape’

'escape'

Instance Attribute Summary collapse

Instance Method Summary collapse

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)



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/tenjin.rb', line 327

def initialize(filename=nil, options={})
  if filename.is_a?(Hash)
    options = filename
    filename = nil
  end
  @filename   = filename
  @escapefunc = options[:escapefunc] || ESCAPE_FUNCTION
  @preamble   = options[:preamble]  == true ? "_buf = #{init_buf_expr()}; " : options[:preamble]
  @postamble  = options[:postamble] == true ? "_buf.to_s"   : options[:postamble]
  @args       = nil  # or array of argument names
  convert_file(filename) if filename
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



340
341
342
# File 'lib/tenjin.rb', line 340

def args
  @args
end

#escapefuncObject

Returns the value of attribute escapefunc.



339
340
341
# File 'lib/tenjin.rb', line 339

def escapefunc
  @escapefunc
end

#filenameObject

Returns the value of attribute filename.



339
340
341
# File 'lib/tenjin.rb', line 339

def filename
  @filename
end

#initbufObject

Returns the value of attribute initbuf.



339
340
341
# File 'lib/tenjin.rb', line 339

def initbuf
  @initbuf
end

#newlineObject

Returns the value of attribute newline.



339
340
341
# File 'lib/tenjin.rb', line 339

def newline
  @newline
end

#scriptObject

,:bytecode



341
342
343
# File 'lib/tenjin.rb', line 341

def script
  @script
end

#timestampObject

Returns the value of attribute timestamp.



340
341
342
# File 'lib/tenjin.rb', line 340

def timestamp
  @timestamp
end

Instance Method Details

#convert(input, filename = nil) ⇒ Object

convert string into ruby code



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/tenjin.rb', line 349

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



344
345
346
# File 'lib/tenjin.rb', line 344

def convert_file(filename)
  return convert(File.read(filename), filename)
end

#init_buf_exprObject

:nodoc:



606
607
608
# File 'lib/tenjin.rb', line 606

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.



612
613
614
615
616
# File 'lib/tenjin.rb', line 612

def render(context=Context.new)
  context = Context.new(context) if context.is_a?(Hash)
  @proc ||= _render()
  return context.instance_eval(&@proc)
end