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

Inherits:
BasicObject
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



18
19
20
# File 'lib/lotus/view/rendering/layout_scope.rb', line 18

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, *args, &blk) ⇒ 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



205
206
207
208
209
210
211
212
213
# File 'lib/lotus/view/rendering/layout_scope.rb', line 205

def method_missing(m, *args, &blk)
  if @scope.respond_to?(m)
    @scope.__send__(m, *args, &blk)
  elsif layout.respond_to?(m)
    layout.__send__(m, *args, &blk)
  else
    ::Lotus::View::Escape.html(super)
  end
end

Instance Method Details

#classObject

Returns the classname as string

Returns:

  • classname

Since:

  • 0.3.0



27
28
29
# File 'lib/lotus/view/rendering/layout_scope.rb', line 27

def class
  (class << self; self end).superclass
end

#content(key) ⇒ String, NilClass

Returns a content for the given key, by trying to invoke on the current scope, a method with the same name.

The scope is made of locals and concrete methods from view and layout.

Examples:

# Given the following layout template

<!doctype HTML>
<html>
  <!-- ... -->
  <body>
    <!-- ... -->
    <%= content :footer %>
  </body>
</html>

# Case 1:
#   Products::Index doesn't respond to #footer, content will return nil
#
# Case 2:
#   Products::Show responds to #footer, content will send back
#     #footer returning value

module Products
  class Index
    include Lotus::View
  end

  class Show
    include Lotus::View

    def footer
      "contents for footer"
    end
  end
end

Parameters:

  • key (Symbol)

    a method to invoke within current scope

Returns:

  • (String, NilClass)

    returning content if scope respond to the requested method

Since:

  • 0.4.1



159
160
161
# File 'lib/lotus/view/rendering/layout_scope.rb', line 159

def content(key)
  __send__(key) if respond_to?(key)
end

#formatSymbol

Returns the requested format.

Returns:

  • (Symbol)

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

Since:

  • 0.1.0



94
95
96
# File 'lib/lotus/view/rendering/layout_scope.rb', line 94

def format
  @scope.format
end

#inspectString

Returns an inspect String

Returns:

  • (String)

    inspect String (contains classname, objectid in hex, available ivars)

Since:

  • 0.3.0



36
37
38
39
40
41
# File 'lib/lotus/view/rendering/layout_scope.rb', line 36

def inspect
  base = "#<#{ self.class }:#{'%x' % (self.object_id << 1)}"
  base << " @layout=\"#{@layout.inspect}\"" if @layout
  base << " @scope=\"#{@scope.inspect}\"" if @scope
  base << ">"
end

#localsHash

The current locals.

Returns:

  • (Hash)

    the current locals

Since:

  • 0.1.0



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

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



85
86
87
# File 'lib/lotus/view/rendering/layout_scope.rb', line 85

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

#respond_to?(m, include_all = false) ⇒ TrueClass, FalseClass

Implements “respond to” logic

Returns:

  • (TrueClass, FalseClass)

See Also:

Since:

  • 0.3.0



170
171
172
# File 'lib/lotus/view/rendering/layout_scope.rb', line 170

def respond_to?(m, include_all = false)
  respond_to_missing?(m, include_all)
end

#respond_to_missing?(m, include_all) ⇒ TrueClass, FalseClass

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.

Implements “respond to” logic

Returns:

  • (TrueClass, FalseClass)

See Also:

Since:

  • 0.3.0



182
183
184
185
# File 'lib/lotus/view/rendering/layout_scope.rb', line 182

def respond_to_missing?(m, include_all)
  @layout.respond_to?(m, include_all) ||
    @scope.respond_to?(m, include_all)
end

#viewLotus::View

The current view.

Returns:

Since:

  • 0.1.0



103
104
105
# File 'lib/lotus/view/rendering/layout_scope.rb', line 103

def view
  @view || @scope.view
end