module Hanami::Extensions::View::Context::ClassExtension::InstanceMethods

@api public @since 2.1.0

Attributes

inflector[R]

Returns the app’s inflector.

@return [Dry::Inflector] the inflector

@api public @since 2.1.0

settings[R]

Returns the app’s settings.

@return [Hanami::Settings] the settings

@api public @since 2.1.0

Public Class Methods

new( inflector: nil, settings: nil, routes: nil, assets: nil, request: nil, **args ) click to toggle source

@see SliceConfiguredContext#define_new

@api private @since 2.1.0

Calls superclass method
# File lib/hanami/extensions/view/context.rb, line 105
def initialize( # rubocop:disable Metrics/ParameterLists
  inflector: nil,
  settings: nil,
  routes: nil,
  assets: nil,
  request: nil,
  **args
)
  @inflector = inflector
  @settings = settings
  @routes = routes
  @assets = assets
  @request = request

  @content_for = {}

  super(**args)
end

Public Instance Methods

assets() click to toggle source

Returns the app’s assets.

@return [Hanami::Assets] the assets

@raise [Hanami::ComponentLoadError] if the hanami-assets gem is not bundled

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 144
def assets
  unless @assets
    msg =
      if Hanami.bundled?("hanami-assets")
        "Have you put files into your assets directory?"
      else
        "The hanami-assets gem is required to access assets."
      end

    raise Hanami::ComponentLoadError, "Assets not available. #{msg}"
  end

  @assets
end
content_for(key, value = nil) { || ... } click to toggle source

@overload content_for(key, value = nil, &block)

Stores a string or block of template markup for later use.

@param key [Symbol] the content key, for later retrieval
@param value [String, nil] the content, if no block is given

@return [String] the content

@example
  content_for(:page_title, "Hello world")

@example In a template
  <%= content_for :page_title do %>
    <h1>Hello world</h1>
  <% end %>

@overload content_for(key)

Returns the previously stored content for the given key.

@param key [Symbol] the content key

@return [String, nil] the content, or nil if no content previously stored with the
  key

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 218
def content_for(key, value = nil)
  if block_given?
    @content_for[key] = yield
    nil
  elsif value
    @content_for[key] = value
    nil
  else
    @content_for[key]
  end
end
csrf_token() click to toggle source

Returns the current request’s CSRF token.

@return [String] the token

@raise [Hanami::ComponentLoadError] if the view is not rendered from within a request @raise [Hanami::Action::MissingSessionError] if sessions are not enabled

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 239
def csrf_token
  request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN]
end
flash() click to toggle source

Returns the flash hash for the current request.

@return []

@raise [Hanami::ComponentLoadError] if the view is not rendered from within a request @raise [Hanami::Action::MissingSessionError] if sessions are not enabled

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 265
def flash
  request.flash
end
initialize_copy(source) click to toggle source

@api private @since 2.1.0

Calls superclass method
# File lib/hanami/extensions/view/context.rb, line 126
def initialize_copy(source)
  # The standard implementation of initialize_copy will make shallow copies of all
  # instance variables from the source. This is fine for most of our ivars.
  super

  # Dup any objects that will be mutated over a given rendering to ensure no leakage of
  # state across distinct view renderings.
  @content_for = source.instance_variable_get(:@content_for).dup
end
request() click to toggle source

Returns the current request, if the view is rendered from within an action.

@return [Hanami::Action::Request] the request

@raise [Hanami::ComponentLoadError] if the view is not rendered from within a request

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 167
def request
  unless @request
    raise Hanami::ComponentLoadError, "Request not available. Only views rendered from Hanami::Action instances have a request."
  end

  @request
end
routes() click to toggle source

Returns the app’s routes helper.

@return [Hanami::Slice::RoutesHelper] the routes helper

@raise [Hanami::ComponentLoadError] if the hanami-router gem is not bundled or routes

are not defined

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 184
def routes
  unless @routes
    raise Hanami::ComponentLoadError, "the hanami-router gem is required to access routes"
  end

  @routes
end
session() click to toggle source

Returns the session for the current request.

@return [Rack::Session::Abstract::SessionHash] the session hash

@raise [Hanami::ComponentLoadError] if the view is not rendered from within a request @raise [Hanami::Action::MissingSessionError] if sessions are not enabled

@api public @since 2.1.0

# File lib/hanami/extensions/view/context.rb, line 252
def session
  request.session
end