Module: Mullet::Sinatra::Helpers

Defined in:
lib/mullet/sinatra.rb

Constant Summary collapse

@@engine =
Mullet::Sinatra::Engine.new()

Instance Method Summary collapse

Instance Method Details

#mullet(view_name, options = {}, locals = {}) ⇒ String

Renders output for a view

Parameters:

  • view_name (Symbol)

    name of view to render

  • options (Hash) (defaults to: {})

    name to value hash of options: :layout

    If value is `false`, no layout is used, otherwise the
    value is the layout name to use.
    

    :locals

    name to value hash of local variables to make available
    to the view
    
  • locals (Hash) (defaults to: {})

    name to value hash of local variables to make available to the view

Returns:

  • (String)

    rendered output



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mullet/sinatra.rb', line 54

def mullet(view_name, options={}, locals={})
  # The options hash may contain a key :locals with the value being a
  # hash mapping variable names to values.
  locals.merge!(options.delete(:locals) || {})

  # Get application settings.
  view_options = { root: settings.root, views: settings.views }

  if settings.respond_to?(:mullet)
    view_options = settings.mullet.merge(view_options)
  end

  view_options.merge!(options)

  # Copy instance variables set by Sinatra application.
  application_data = Object.new()
  instance_variables.each do |name|
    application_data.instance_variable_set(
        name, instance_variable_get(name))
  end

  # Render view.
  template = @@engine.get_template(view_name, view_options)
  view_class = @@engine.get_view_class(view_name, view_options)
  view = view_class.new()
  view.set_model(application_data, locals)
  output = ''
  template.execute(view, output)

  # Render layout.
  layout_name = :layout
  if options[:layout]
    layout_name = options[:layout]
  end

  if layout_name != false
    # If configured layout is true or nil, then use :layout.
    if layout_name == true || !layout_name
      layout_name = :layout
    end

    layout = @@engine.get_template(layout_name, view_options)
    view_class = @@engine.get_view_class(layout_name, view_options)
    view = view_class.new()
    view.set_model(application_data, locals, { content: output })
    layout_output = ''
    layout.execute(view, layout_output)
    output = layout_output
  end

  return output
end