Class: Brochure::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, path) ⇒ Template

Returns a new instance of Template.



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

def initialize(app, path)
  @app  = app
  @path = path
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/brochure/template.rb', line 3

def app
  @app
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/brochure/template.rb', line 3

def path
  @path
end

Instance Method Details

#basenameObject



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

def basename
  @basename ||= File.basename(path)
end

#content_typeObject



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

def content_type
  @content_type ||= begin
    type = Rack::Mime.mime_type(format_extension)
    type[/^text/] ? "#{type}; charset=utf-8" : type
  end
end

#engine_extensionObject



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

def engine_extension
  extensions[1]
end

#extensionsObject



21
22
23
# File 'lib/brochure/template.rb', line 21

def extensions
  @extensions ||= basename.scan(/\.[^.]+/)
end

#format_extensionObject



25
26
27
# File 'lib/brochure/template.rb', line 25

def format_extension
  extensions.first
end

#render(env, locals = {}, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/brochure/template.rb', line 40

def render(env, locals = {}, &block)
  if template
    template.render(app.context_for(self, env), locals, &block)
  else
    File.read(path)
  end
end

#templateObject



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

def template
  if engine_extension
    options = app.template_options[engine_extension] || nil
    @template ||= Tilt.new(path, options, :outvar => '@_out_buf')
  end
end