Module: Hanami::Extensions::View::Context::ClassExtension::InstanceMethods

Defined in:
lib/hanami/extensions/view/context.rb

Overview

Since:

  • 2.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inflectorDry::Inflector (readonly)

Returns the app’s inflector.

Returns:

  • (Dry::Inflector)

    the inflector

Since:

  • 2.1.0



91
92
93
# File 'lib/hanami/extensions/view/context.rb', line 91

def inflector
  @inflector
end

#settingsHanami::Settings (readonly)

Returns the app’s settings.

Returns:

Since:

  • 2.1.0



99
100
101
# File 'lib/hanami/extensions/view/context.rb', line 99

def settings
  @settings
end

Instance Method Details

#assetsHanami::Assets

Returns the app’s assets.

Returns:

  • (Hanami::Assets)

    the assets

Raises:

Since:

  • 2.1.0



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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, &block) ⇒ String #content_for(key) ⇒ String?

Overloads:

  • #content_for(key, value = nil, &block) ⇒ String

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

    Examples:

    content_for(:page_title, "Hello world")

    In a template

    <%= content_for :page_title do %>
      <h1>Hello world</h1>
    <% end %>

    Parameters:

    • key (Symbol)

      the content key, for later retrieval

    • value (String, nil) (defaults to: nil)

      the content, if no block is given

    Returns:

    • (String)

      the content

  • #content_for(key) ⇒ String?

    Returns the previously stored content for the given key.

    Parameters:

    • key (Symbol)

      the content key

    Returns:

    • (String, nil)

      the content, or nil if no content previously stored with the key

Since:

  • 2.1.0



218
219
220
221
222
223
224
225
226
227
228
# 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_tokenString

Returns the current request’s CSRF token.

Returns:

  • (String)

    the token

Raises:

  • (Hanami::ComponentLoadError)

    if the view is not rendered from within a request

  • (Hanami::Action::MissingSessionError)

    if sessions are not enabled

Since:

  • 2.1.0



239
240
241
# File 'lib/hanami/extensions/view/context.rb', line 239

def csrf_token
  request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN]
end

#flashObject

Returns the flash hash for the current request.

Returns:

Raises:

  • (Hanami::ComponentLoadError)

    if the view is not rendered from within a request

  • (Hanami::Action::MissingSessionError)

    if sessions are not enabled

Since:

  • 2.1.0



265
266
267
# File 'lib/hanami/extensions/view/context.rb', line 265

def flash
  request.flash
end

#initialize(inflector: nil, settings: nil, routes: nil, assets: nil, request: nil, **args) ⇒ Object

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.

See Also:

  • SliceConfiguredContext#define_new

Since:

  • 2.1.0



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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

#initialize_copy(source) ⇒ Object

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.

Since:

  • 2.1.0



126
127
128
129
130
131
132
133
134
# 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

#requestHanami::Action::Request

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

Returns:

  • (Hanami::Action::Request)

    the request

Raises:

Since:

  • 2.1.0



167
168
169
170
171
172
173
# 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

#routesHanami::Slice::RoutesHelper

Returns the app’s routes helper.

Returns:

Raises:

Since:

  • 2.1.0



184
185
186
187
188
189
190
# 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

#sessionRack::Session::Abstract::SessionHash

Returns the session for the current request.

Returns:

  • (Rack::Session::Abstract::SessionHash)

    the session hash

Raises:

  • (Hanami::ComponentLoadError)

    if the view is not rendered from within a request

  • (Hanami::Action::MissingSessionError)

    if sessions are not enabled

Since:

  • 2.1.0



252
253
254
# File 'lib/hanami/extensions/view/context.rb', line 252

def session
  request.session
end