Module: Cuba::Render
- Defined in:
- lib/cuba/render.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_cache ⇒ Object
- #_render(template, locals = {}, options = {}, &block) ⇒ Object
- #partial(template, locals = {}) ⇒ Object
- #render(template, locals = {}, layout = ) ⇒ Object
- #template_path(template) ⇒ Object
- #view(template, locals = {}, layout = ) ⇒ Object
Class Method Details
.setup(app) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/cuba/render.rb', line 5 def self.setup(app) app.settings[:render] ||= {} app.settings[:render][:template_engine] ||= "erb" app.settings[:render][:layout] ||= "layout" app.settings[:render][:views] ||= File.("views", Dir.pwd) app.settings[:render][:options] ||= { default_encoding: Encoding.default_external } end |
Instance Method Details
#_cache ⇒ Object
59 60 61 |
# File 'lib/cuba/render.rb', line 59 def _cache Thread.current[:_cache] ||= Tilt::Cache.new end |
#_render(template, locals = {}, options = {}, &block) ⇒ Object
51 52 53 54 55 |
# File 'lib/cuba/render.rb', line 51 def _render(template, locals = {}, = {}, &block) _cache.fetch(template) { Tilt.new(template, 1, .merge(outvar: '@_output')) }.render(self, locals, &block) end |
#partial(template, locals = {}) ⇒ Object
24 25 26 |
# File 'lib/cuba/render.rb', line 24 def partial(template, locals = {}) _render(template_path(template), locals, settings[:render][:options]) end |
#render(template, locals = {}, layout = ) ⇒ Object
15 16 17 18 |
# File 'lib/cuba/render.rb', line 15 def render(template, locals = {}, layout = settings[:render][:layout]) res.headers["Content-Type"] ||= "text/html; charset=utf-8" res.write(view(template, locals, layout)) end |
#template_path(template) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/cuba/render.rb', line 28 def template_path(template) dir = settings[:render][:views] ext = settings[:render][:template_engine] return File.join(dir, "#{ template }.#{ ext }") end |
#view(template, locals = {}, layout = ) ⇒ Object
20 21 22 |
# File 'lib/cuba/render.rb', line 20 def view(template, locals = {}, layout = settings[:render][:layout]) partial(layout, locals.merge(content: partial(template, locals))) end |