Class: Template

Inherits:
Object
  • Object
show all
Defined in:
lib/template.rb,
lib/template/node.rb,
lib/template/parser.rb,
lib/template/node/part.rb,
lib/template/node/template.rb,
lib/template/node/code_part.rb,
lib/template/node/text_part.rb,
lib/template/parser/template.rb

Defined Under Namespace

Classes: Node, Parser

Constant Summary collapse

VERSION =
Gem::Version.new("0.2.0")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, io: StringIO.new) ⇒ Template

Returns a new instance of Template.



4
5
6
7
8
# File 'lib/template.rb', line 4

def initialize(input, io: StringIO.new)
  @input = input
  @parsed = ::Template::Parser::Template.new.parse(@input)
  @io = io
end

Class Method Details

.render(input, context = "", io: StringIO.new) ⇒ Object



10
11
12
# File 'lib/template.rb', line 10

def self.render(input, context = "", io: StringIO.new)
  new(input, io: io).render(context)
end

Instance Method Details

#render(context = "") ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/template.rb', line 14

def render(context = "")
  if context.present?
    context = ::Code.evaluate(context)
  else
    context = ::Code::Object::Dictionnary.new
  end

  ::Template::Node::Template.new(@parsed).evaluate(context: context, io: @io)

  @io.is_a?(StringIO) ? @io.string : nil
end