Class: Zafu::Template

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

Instance Method Summary collapse

Constructor Details

#initialize(template, src_helper = nil, compiler = Zafu::Compiler) ⇒ Template

Returns a new instance of Template.



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

def initialize(template, src_helper = nil, compiler = Zafu::Compiler)
  if template.kind_of?(String)
    @ast = compiler.new(template)
  else
    @ast = compiler.new_with_url(template.path, :helper => src_helper)
  end
end

Instance Method Details

#to_erb(context = {}) ⇒ Object



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

def to_erb(context = {})
  @ast.to_erb(context)
end

#to_ruby(context = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/zafu/template.rb', line 15

def to_ruby(context = {})
  src = ::ERB.new("<% __in_erb_template=true %>#{to_erb(context)}", nil, '-').src

  # Ruby 1.9 prepends an encoding to the source. However this is
  # useless because you can only set an encoding on the first line
  RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
end