Class: Hanami::View::Rendering::LayoutScope

Inherits:
Utils::BasicObject
Defined in:
lib/hanami/ruby3/hanami/view/rendering/layout_scope.rb

Direct Known Subclasses

Scope

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m_name, *args, **kwargs, &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.

rubocop:disable Style/MissingRespondToMissing, Lint/DuplicateBranch

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hanami/ruby3/hanami/view/rendering/layout_scope.rb', line 28

def method_missing(m_name, *args, **kwargs, &blk)
  # FIXME: this isn't compatible with Hanami 2.0, as it extends a view
  # that we want to be frozen in the future
  #
  # See https://github.com/hanami/view/issues/130#issuecomment-319326236
  if @scope.respond_to?(m_name, true) && @scope.locals.key?(m_name) && layout.respond_to?(m_name, true)
    layout.__send__(m_name, *args, **kwargs, &blk)
  elsif @scope.respond_to?(m_name, true)
    @scope.__send__(m_name, *args, **kwargs, &blk)
  elsif layout.respond_to?(m_name, true)
    layout.__send__(m_name, *args, **kwargs, &blk)
  else
    ::Hanami::View::Escape.html(super)
  end
end