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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

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

Constructor Details

#initialize(app, file) ⇒ Template

Returns a new instance of Template.



31
32
33
34
35
36
# File 'lib/kontrol/template.rb', line 31

def initialize(app, file)
  @app = app
  @file = file
  
  load
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



29
30
31
# File 'lib/kontrol/template.rb', line 29

def file
  @file
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/kontrol/template.rb', line 43

def changed?
  File.mtime(@file) != @mtime
end

#loadObject



38
39
40
41
# File 'lib/kontrol/template.rb', line 38

def load
  @mtime = File.mtime(file)
  @erb = ERB.new(File.read(file))
end

#render(variables) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kontrol/template.rb', line 47

def render(variables)
  load if ENV['RACK_ENV'] != 'production' and changed?
  
  @erb.result(View.new(variables.merge(:application => @app)).__binding__)

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