Module: Cuba::Render

Defined in:
lib/cuba/render.rb

Class Method Summary collapse

Instance Method Summary collapse

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.expand_path("views", Dir.pwd)
  app.settings[:render][:options] ||= {
    default_encoding: Encoding.default_external
  }
end

Instance Method Details

#partial(template, locals = {}) ⇒ Object



27
28
29
# File 'lib/cuba/render.rb', line 27

def partial(template, locals = {})
  render(template_path(template), locals, settings[:render][:options])
end

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

Render any type of template file supported by Tilt.

Examples:


# Renders home, and is assumed to be HAML.
render("home.haml")

# Renders with some local variables
render("home.haml", site_name: "My Site")

# Renders with HAML options
render("home.haml", {}, ugly: true, format: :html5)

# Renders in layout
render("layout.haml") { render("home.haml") }


47
48
49
50
51
# File 'lib/cuba/render.rb', line 47

def render(template, locals = {}, options = {}, &block)
  _cache.fetch(template) {
    Tilt.new(template, 1, options.merge(outvar: '@_output'))
  }.render(self, locals, &block)
end

#template_path(template) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cuba/render.rb', line 19

def template_path(template)
  "%s/%s.%s" % [
    settings[:render][:views],
    template,
    settings[:render][:template_engine]
  ]
end

#view(template, locals = {}, layout = ) ⇒ Object



15
16
17
# File 'lib/cuba/render.rb', line 15

def view(template, locals = {}, layout = settings[:render][:layout])
  partial(layout, { content: partial(template, locals) }.merge(locals))
end