class Racket::Utils::Views::Renderer

Class responsible for rendering a controller/view/layout combination.

Public Class Methods

render(controller, view, layout) click to toggle source

Renders a page using the provided controller/view and layout combination and returns an response array that can be sent to the client.

@param [Racket::Controller] controller @param [String] view @param [String] layout @return [Array]

# File lib/racket/utils/views/renderer.rb, line 39
def self.render(controller, view, layout)
  send_response(
    controller.response,
    view ? render_template(controller, view, layout) : controller.racket.action_result
  )
end
service(_options = {}) click to toggle source

Returns a service proc that can be used by the registry.

@param [Hash] _options (unused) @return [Proc]

# File lib/racket/utils/views/renderer.rb, line 28
def self.service(_options = {})
  -> { self }
end

Private Class Methods

render_template(controller, view, layout) click to toggle source

Renders a template/layout combo using Tilt and returns it as a string.

@param [Racket::Controller] controller @param [String] view @param [String|nil] layout @return [String]

# File lib/racket/utils/views/renderer.rb, line 52
def self.render_template(controller, view, layout)
  output = Tilt.new(view, nil, controller.view_settings).render(controller)
  output =
    Tilt.new(
      layout, nil, controller.layout_settings
    ).render(controller) { output } if layout
  output
end
send_response(response, output) click to toggle source

Sends response to client.

@param [Racket::Response] response @param [String] output @return nil

# File lib/racket/utils/views/renderer.rb, line 66
def self.send_response(response, output)
  response.write(output)
  response.finish
end