Class: Lotus::View::Rendering::LayoutScope

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/view/rendering/layout_scope.rb

Overview

Scope for layout rendering

Since:

  • 0.1.0

Direct Known Subclasses

Scope

Instance Method Summary collapse

Constructor Details

#initialize(layout, scope) ⇒ LayoutScope

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the scope

Parameters:

Since:

  • 0.1.0



16
17
18
# File 'lib/lotus/view/rendering/layout_scope.rb', line 16

def initialize(layout, scope)
  @layout, @scope = layout, scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Forward all the missing methods to the view scope or to the layout.

Examples:

# In the layout template:
#   templates/application.html.erb
#
# Use like this:
<title><%= article.title %></title>

# `article` will be looked up in the view scope first.
# If not found, it will be searched within the layout.

See Also:

Since:

  • 0.1.0



111
112
113
114
115
116
117
# File 'lib/lotus/view/rendering/layout_scope.rb', line 111

def method_missing(m)
  begin
    @scope.__send__ m
  rescue
    @layout.__send__ m
  end
end

Instance Method Details

#formatSymbol

Returns the requested format.

Returns:

  • (Symbol)

    the requested format (eg. :html, :json, :xml, etc..)

Since:

  • 0.1.0



71
72
73
# File 'lib/lotus/view/rendering/layout_scope.rb', line 71

def format
  @scope.format
end

#localsHash

The current locals.

Returns:

  • (Hash)

    the current locals

Since:

  • 0.1.0



89
90
91
# File 'lib/lotus/view/rendering/layout_scope.rb', line 89

def locals
  @locals || @scope.locals
end

#render(options) ⇒ String

Render a partial or a template within a layout template.

Examples:

Rendering partial

# Given a partial under:
#   templates/shared/_sidebar.html.erb
#
# In the layout template:
#   templates/application.html.erb
#
# Use like this:
<%= render partial: 'shared/sidebar' %>

Rendering template

# Given a template under:
#   templates/articles/index.html.erb
#
# In the layout template:
#   templates/application.html.erb
#
# Use like this:
<%= render template: 'articles/index' %>

Rendering partial, using optional :locals

# Given a partial under:
#   templates/shared/_sidebar.html.erb
#
# In the layout template:
#   templates/application.html.erb
#
# Use like this:
<%= render partial: 'shared/sidebar', { user: current_user } %>

#
# `user` will be available in the scope of the sidebar rendering

Parameters:

  • options (Hash)

Options Hash (options):

  • :partial (String)

    the partial template to render

  • :template (String)

    the template to render

Returns:

  • (String)

    the output of the rendering process

Since:

  • 0.1.0



62
63
64
# File 'lib/lotus/view/rendering/layout_scope.rb', line 62

def render(options)
  renderer(options).render
end

#viewLotus::View

The current view.

Returns:

Since:

  • 0.1.0



80
81
82
# File 'lib/lotus/view/rendering/layout_scope.rb', line 80

def view
  @view || @scope.view
end