Class: Brochure::Template
- Inherits:
-
Object
- Object
- Brochure::Template
- Defined in:
- lib/brochure/template.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #basename ⇒ Object
- #content_type ⇒ Object
- #engine_extension ⇒ Object
- #extensions ⇒ Object
- #format_extension ⇒ Object
-
#initialize(app, path) ⇒ Template
constructor
A new instance of Template.
- #render(env, locals = {}, &block) ⇒ Object
- #template ⇒ Object
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
#app ⇒ Object (readonly)
Returns the value of attribute app.
3 4 5 |
# File 'lib/brochure/template.rb', line 3 def app @app end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/brochure/template.rb', line 3 def path @path end |
Instance Method Details
#basename ⇒ Object
17 18 19 |
# File 'lib/brochure/template.rb', line 17 def basename @basename ||= File.basename(path) end |
#content_type ⇒ Object
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_extension ⇒ Object
29 30 31 |
# File 'lib/brochure/template.rb', line 29 def engine_extension extensions[1] end |
#extensions ⇒ Object
21 22 23 |
# File 'lib/brochure/template.rb', line 21 def extensions @extensions ||= basename.scan(/\.[^.]+/) end |
#format_extension ⇒ Object
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 |
#template ⇒ Object
10 11 12 13 14 15 |
# File 'lib/brochure/template.rb', line 10 def template if engine_extension = app.[engine_extension] || nil @template ||= Tilt.new(path, , :outvar => '@_out_buf') end end |