Class: FunApi::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/funapi/templates.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory:, layout: nil) ⇒ Templates

Returns a new instance of Templates.



10
11
12
13
14
# File 'lib/funapi/templates.rb', line 10

def initialize(directory:, layout: nil)
  @directory = Pathname.new(directory)
  @layout = layout
  @cache = {}
end

Instance Method Details

#render(name, layout: nil, **context) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/funapi/templates.rb', line 20

def render(name, layout: nil, **context)
  content = render_template(name, **context)

  layout_to_use = determine_layout(layout)
  if layout_to_use
    render_with_layout(layout_to_use, content, **context)
  else
    content
  end
end

#render_partial(name, **context) ⇒ Object



36
37
38
# File 'lib/funapi/templates.rb', line 36

def render_partial(name, **context)
  render_template(name, **context)
end

#response(name, status: 200, headers: {}, layout: nil, **context) ⇒ Object



31
32
33
34
# File 'lib/funapi/templates.rb', line 31

def response(name, status: 200, headers: {}, layout: nil, **context)
  html = render(name, layout: layout, **context)
  TemplateResponse.new(html, status: status, headers: headers)
end

#with_layout(layout) ⇒ Object



16
17
18
# File 'lib/funapi/templates.rb', line 16

def with_layout(layout)
  ScopedTemplates.new(self, layout)
end