class

Azu::Helpers::TemplateContext

Inherits Reference < Object

TemplateContext wraps the HTTP server context for use in templates.

This class provides convenient access to request information, flash messages, session data, cookies, and CSRF tokens within Crinja templates.

Usage

TemplateContext is automatically created and injected when rendering views via the Renderable module. It provides access to:

  • Current request path and URL
  • Query parameters
  • Cookies
  • Flash messages
  • Session data
  • CSRF tokens

Example

In templates:

<p>Current path: {{ current_path }}</p>
<p>CSRF Token: {{ csrf_token }}</p>
{% if flash.notice %}
  <div class="alert">{{ flash.notice }}</div>
{% endif %}

Constructors

Instance methods

accept_language

Returns the Accept-Language header locale.

Source
ajax?

Check if the request is an AJAX request.

Source
body

Returns the request body as string.

Source
client_ip

Returns the client IP address.

Source
context
Source
cookies
Source
csrf_metatag

Returns CSRF meta tag for AJAX requests.

{{ csrf_metatag }}
Source
csrf_tag

Returns CSRF hidden input tag.

{{ csrf_tag }}
Source
csrf_token

Returns CSRF token for the current request.

<input type="hidden" name="_csrf" value="{{ csrf_token }}" />
Source
current_page?(path : String, exact : Bool = true) : Bool

Check if the current path matches.

ctx.current_page?("/users")               # => true if on /users
ctx.current_page?("/users", exact: false) # => true if path starts with /users
Source
current_path

Returns the current request path.

ctx.current_path # => "/users/123"
Source
current_url

Returns the full current URL.

ctx.current_url # => "https://example.com/users/123?page=1"
Source
flash

Returns flash messages from cookie.

Flash messages are typically set after redirects to display one-time notifications.

{% if flash.notice %}
  <div class="alert alert-success">{{ flash.notice }}</div>
{% endif %}
{% if flash.error %}
  <div class="alert alert-danger">{{ flash.error }}</div>
{% endif %}
Source
headers

Returns request headers.

Source
host

Returns the request host.

Source
method

Returns the HTTP method.

Source
params
Source
referer

Returns the referer URL.

Source
request
Source
response
Source
scheme

Returns the request scheme (http or https).

Source
secure?

Check if the request is a secure (HTTPS) request.

Source
session

Returns session data.

Session data is extracted from cookies and decoded.

Source
to_template_vars

Convert context to a hash for template rendering.

Source
user_agent

Returns the user agent string.

Source