Module: LeanWeb::ControllerMixins::RenderWithLayout

Defined in:
contrib/lib/leanweb/controller_mixins/render_with_layout.rb

Overview

#render_with_layout for LeanWeb::Controller with ERB, HAML, Tilt::EmacsOrg and Redcarpet Markdown support.

Instance Method Summary collapse

Instance Method Details

#default_static_action(view_path) ⇒ Object

Render response for missing static action methods. Called from Route#respond.



59
60
61
# File 'contrib/lib/leanweb/controller_mixins/render_with_layout.rb', line 59

def default_static_action(view_path)
  render_with_layout(view_path)
end

#render_with_layout(path, locals: {}, title: nil, head: nil, layout: 'layout.haml', sub_layout: nil, options: nil) { ... } ⇒ Object

Render a response with a layout.

Depending on the path file contents and options @content_for might be filled. These contents must be inserted on the head or body of your layout file.

All paths used on parameters can be relative to VIEW_PATH or absolute.

Parameters:

  • path (String)

    Path to file to rendered.

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

    Local variables for rendered document.

  • title (String) (defaults to: nil)

    @content_for[:title] content.

  • head (Hash) (defaults to: nil)

    Things to be inserted on @content_for[:head].

  • layout (String) (defaults to: 'layout.haml')

    Layout file path.

  • sub_layout (String) (defaults to: nil)

    Layout file to be inserted within layout.

  • options (Hash) (defaults to: nil)

    Options hash, to be given to the template engine based on file extension: Tilt::EmacsOrg for .org and Redcarpet for .md.

Options Hash (head:):

  • :js (String|Array)

    Javascript file path(s).

  • :jsm (String|Array)

    Javascript module file path (s).

  • :css (String|Array)

    CSS file path(s).

  • :raw (String|Array)

    Raw headers in HTML format.

Options Hash (options:):

  • :setupfile (String)

    For .org.

  • :toc (true)

    For .md. To fill @content_for[:toc] render with option :with_toc_data.

Yields:

  • On dynamic templates (such as Haml and ERB) you can yield.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'contrib/lib/leanweb/controller_mixins/render_with_layout.rb', line 41

def render_with_layout( # rubocop:disable Metrics/ParameterLists
  path,
  locals: {},
  title: nil,
  head: nil,
  layout: 'layout.haml',
  sub_layout: nil,
  options: nil,
  &block
)
  @content_for[:title] = title unless title.nil?
  content = render_by_extension(path, locals, sub_layout, options, &block)
  prepare_head(**head) unless head.nil?
  render_response(layout, locals, 'text/html'){ content }
end