Azu::Helpers::TemplateContext
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
new(context : HTTP::Server::Context)
SourceInstance methods
context
Sourcecookies
Sourcecsrf_token
Returns CSRF token for the current request.
<input type="hidden" name="_csrf" value="{{ csrf_token }}" />
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
current_url
Returns the full current URL.
ctx.current_url # => "https://example.com/users/123?page=1"
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 %}
params
Sourcerequest
Sourceresponse
Source