Class: Flame::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/render.rb

Overview

Helper for render functionality

Instance Method Summary collapse

Constructor Details

#initialize(controller, path, options = {}) ⇒ Render

Create a new instance from controller, by path and with options

Parameters:

  • controller for default scope, views directory and cache

  • path (full or the last part) for view search

  • (defaults to: {})

    options for template

Options Hash (options):

  • :scope (Object) — default: controller

    scope of visibility in rendering

  • :layout (Symbol, String, false) — default: 'layout.*'

    name of the layout file

  • :tilt (Hash)

    options for Tilt

  • :locals (Hash) — default: {}

    local variables for rendering

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flame/render.rb', line 27

def initialize(controller, path, options = {})
  ## Take options for rendering
  @controller = controller
  @scope = options.delete(:scope) { @controller }
  @layout = options.delete(:layout) { 'layout.*' }

  ## Options for Tilt Template
  @tilt_options = options.delete(:tilt)

  ## And get the rest variables to locals
  @locals = options.merge(options.delete(:locals) { {} })

  ## Find filename
  @filename = find_file(path)
  raise Flame::Errors::TemplateNotFoundError.new(controller, path) unless @filename

  @layout = nil if File.basename(@filename)[0] == '_'
end

Instance Method Details

#render(cache: true) ⇒ String

Render template with layout

Parameters:

  • (defaults to: true)

    cache compiles or not

Returns:

  • compiled template



49
50
51
52
53
54
55
56
57
# File 'lib/flame/render.rb', line 49

def render(cache: true, &)
  @cache = cache
  ## Compile Tilt to instance hash
  return unless @filename

  tilt = compile_file
  ## Render Tilt from instance hash with new options
  layout_render tilt.render(@scope, @locals, &)
end