Class: Kontrol::Template

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/kontrol/template.rb

Overview

This class renders an ERB template for a set of attributes, which are accessible as instance variables.

Constant Summary

Constants included from Helpers

Helpers::HTML_ESCAPE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#h, #link_to, #markdown, #strip_tags, #tag, #urlify

Constructor Details

#initialize(app, vars) ⇒ Template

Initialize this template with an ERB instance.



9
10
11
12
13
14
15
# File 'lib/kontrol/template.rb', line 9

def initialize(app, vars)
  @__app__ = app
  
  vars.each do |k, v|
    instance_variable_set "@#{k}", v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/kontrol/template.rb', line 33

def method_missing(id, *args, &block)
  if @__app__.respond_to?(id)
    return @__app__.send(id, *args, &block)
  end
  super
end

Class Method Details

.render(erb, app, file, vars) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kontrol/template.rb', line 21

def self.render(erb, app, file, vars)
  template = Template.new(app, vars)
  
  return erb.result(template.__binding__)

rescue => e
  e.backtrace.each do |s|
    s.gsub!('(erb)', file)
  end
  raise e
end

Instance Method Details

#__binding__Object



17
18
19
# File 'lib/kontrol/template.rb', line 17

def __binding__
  binding
end