Class: Hanami::View::Rendering::NullTemplate Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/view/rendering/null_template.rb

Overview

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

Null Object pattern for layout template

It’s used when a layout doesn’t have an associated template.

A common scenario is for non-html requests. Usually we have a template for the application layout (eg ‘templates/application.html.erb`), but we don’t use to have the template for JSON requests (eg ‘templates/application.json.erb`). Because most of the times, we only return the output of the view.

Examples:

require 'hanami/view'

# We have an ApplicationLayout (views/application_layout.rb):
class ApplicationLayout
  include Hanami::Layout
end

# Our layout has a template for HTML requests, located at:
# templates/application.html.erb

# We set it as global layout
Hanami::View.layout = :application

# We have two views for HTML and JSON articles.
# They have a template each:
#
#   * templates/articles/show.html.erb
#   * templates/articles/show.json.erb
module Articles
  class Show
    include Hanami::View
    format :html
  end

  class JsonShow < Show
    format :json
  end
end

# We initialize the framework
Hanami::View.load!

# When we ask for a HTML rendering, it will use `Articles::Show` and
# ApplicationLayout. The output will be a composition of:
#
#   * templates/articles/show.html.erb
#   * templates/application.html.erb

# When we ask for a JSON rendering, it will use `Articles::JsonShow`
# and ApplicationLayout. Since, the layout doesn't have any associated
# template for JSON, the output will be a composition of:
#
#   * templates/articles/show.json.erb

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#render(scope, locals = {}) {|Proc| ... } ⇒ String

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.

Render the layout template

Parameters:

  • scope (Hanami::View::Scope)

    the rendering scope

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

    a set of objects available during the rendering

Yields:

  • (Proc)

    yields the given block

Returns:

  • (String)

    the output of the rendering process

See Also:

Since:

  • 0.1.0



77
78
79
# File 'lib/hanami/view/rendering/null_template.rb', line 77

def render(scope, locals = {})
  yield
end