Class: Ruty::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ruty.rb

Overview

template class

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Template

load a template from a sourcecode or nodelist.


426
427
428
429
430
431
432
# File 'lib/ruty.rb', line 426

def initialize source
  if source.is_a?(Datastructure::NodeList)
    @nodelist = source
  else
    @nodelist = Parser.new(source).parse
  end
end

Instance Method Details

#render(namespace) ⇒ Object

render the template. Pass it a hash or hashlike object (must support [] and has_key?) which is used as data storage for the root namespace


437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/ruty.rb', line 437

def render namespace
  context = Context.new(namespace)
  context.push(
    :ruty =>      RUTY_CONTEXT,
    :nil =>       nil,
    :true =>      true,
    :false =>     false
  )
  result = ''
  @nodelist.render_node(context, result)
  result
end