class Athena::Framework::View(T)
inherits Reference
#
An ATH::View
represents an ATH::Response
, but in a format agnostic way.
Returning a ATH::View
is essentially the same as returning the data directly; but allows customizing
the response status and headers without needing to render the response body within the controller as an ATH::Response
.
require "athena"
class HelloController < ATH::Controller
@[ARTA::Get("/{name}")]
def say_hello(name : String) : NamedTuple(greeting: String)
{greeting: "Hello #{name}"}
end
@[ARTA::Get("/view/{name}")]
def say_hello_view(name : String) : ATH::View(NamedTuple(greeting: String))
self.view({greeting: "Hello #{name}"}, :im_a_teapot)
end
end
ATH.run
# GET /Fred # => 200 {"greeting":"Hello Fred"}
# GET /view/Fred # => 418 {"greeting":"Hello Fred"}
See the Getting Started docs for more information.
Included modules
Athena::Framework::ViewBase
Constructors#
.create_redirect(url : String, status : HTTP::Status = HTTP::Status::FOUND, headers : HTTP::Headers = HTTP::Headers.new) : self
#
Creates a view instance that'll redirect to the provided url. See #location
.
Optionally allows setting the underlying status and/or headers.
.create_route_redirect(route : String, params : Hash(String, _) = Hash(String, String | ::Nil).new, status : HTTP::Status = HTTP::Status::FOUND, headers : HTTP::Headers = HTTP::Headers.new) : self
#
Creates a view instance that'll redirect to the provided route. See #route
.
Optionally allows setting the underlying route params, status, and/or headers.
.new(data : T | Nil = nil, status : HTTP::Status | Nil = nil, headers : HTTP::Headers = HTTP::Headers.new)
#
Methods#
#format : String | ::Nil
#
The format the view should be rendered in.
The format must be registered with the ATH::Request::FORMATS
hash;
either as a built in format, or a custom one that has registered via ATH::Request.register_format
.
#format=(format : String | Nil)
#
The format the view should be rendered in.
The format must be registered with the ATH::Request::FORMATS
hash;
either as a built in format, or a custom one that has registered via ATH::Request.register_format
.
#headers=(headers : HTTP::Headers) : Nil
#
Sets the headers that should be returned as part of the underlying #response
.
#location : String | ::Nil
#
Returns the URL
that the current request should be redirected to.
See the Location header documentation.
#route : String | ::Nil
#
Returns the name of the route the current request should be redirected to.
See the Getting Started docs for more information.
#route_params : Hash(String, String | ::Nil)
#
The parameters that should be used when constructing the redirect #route
URL.
#route_params=(route_params : Hash(String, String | Nil))
#
The parameters that should be used when constructing the redirect #route
URL.
#set_header(name : String, value : String) : Nil
#
Adds the provided header name and value to the underlying #response
.
#set_header(name : String, value : _) : Nil
#
Adds the provided header name and value to the underlying #response
.