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



94
95
96
# File 'lib/hanami/extensions/view/context.rb', line 94

def inflector
  @inflector
end

Instance Method Details

#assetsHanami::Assets

Returns the app’s assets.

Returns:

  • (Hanami::Assets)

    the assets

Raises:

Since:

  • 2.1.0



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hanami/extensions/view/context.rb', line 139

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



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/hanami/extensions/view/context.rb', line 241

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



262
263
264
# File 'lib/hanami/extensions/view/context.rb', line 262

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



288
289
290
# File 'lib/hanami/extensions/view/context.rb', line 288

def flash
  request.flash
end

#i18nHanami::Providers::I18n::Backend

Returns the slice’s i18n backend.

Returns:

Raises:

Since:

  • x.x.x



207
208
209
210
211
212
213
# File 'lib/hanami/extensions/view/context.rb', line 207

def i18n
  unless @i18n
    raise Hanami::ComponentLoadError, "the i18n gem is required to access translations"
  end

  @i18n
end

#initialize(inflector: nil, routes: nil, assets: nil, request: nil, i18n: 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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hanami/extensions/view/context.rb', line 100

def initialize(
  inflector: nil,
  routes: nil,
  assets: nil,
  request: nil,
  i18n: nil,
  **args
)
  @inflector = inflector
  @routes = routes
  @assets = assets
  @request = request
  @i18n = i18n

  @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



121
122
123
124
125
126
127
128
129
# File 'lib/hanami/extensions/view/context.rb', line 121

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



162
163
164
165
166
167
168
169
170
# File 'lib/hanami/extensions/view/context.rb', line 162

def request
  unless @request
    raise Hanami::ComponentLoadError, <<~STR
      Request not available. Only views rendered from Hanami::Action instances have a request.
    STR
  end

  @request
end

#request?Boolean

Returns true if the view is rendered from within an action and a request is available.

Returns:

  • (Boolean)

Since:

  • 2.3.0



178
179
180
# File 'lib/hanami/extensions/view/context.rb', line 178

def request?
  !!@request
end

#routesHanami::Slice::RoutesHelper

Returns the app’s routes helper.

Returns:

Raises:

Since:

  • 2.1.0



191
192
193
194
195
196
197
# File 'lib/hanami/extensions/view/context.rb', line 191

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



275
276
277
# File 'lib/hanami/extensions/view/context.rb', line 275

def session
  request.session
end