Skip to content

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.

View source

.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.

View source

.new(data : T | Nil = nil, status : HTTP::Status | Nil = nil, headers : HTTP::Headers = HTTP::Headers.new)#

View source

Methods#

#context : ATH::View::Context#

View source

#context=(context : ATH::View::Context)#

View source

#data : T#

The response data.

View source

#data=(data : T)#

The response data.

View source

#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.

View source

#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.

View source

#headers : ATH::Response::Headers#

Returns the headers of the underlying #response.

View source

#headers=(headers : HTTP::Headers) : Nil#

Sets the headers that should be returned as part of the underlying #response.

View source

#location : String | ::Nil#

Returns the URL that the current request should be redirected to.

See the Location header documentation.

View source

#location=(location : String) : Nil#

Sets the redirect #location.

View source

#response : ATH::Response#

The wrapped ATH::Response instance.

View source

#response=(response : ATH::Response)#

The wrapped ATH::Response instance.

View source

#return_type : T.class#

Returns the type of the data represented by self.

View source

#route : String | ::Nil#

Returns the name of the route the current request should be redirected to.

See the Getting Started docs for more information.

View source

#route=(route : String) : Nil#

Sets the redirect #route.

View source

#route_params : Hash(String, String | ::Nil)#

The parameters that should be used when constructing the redirect #route URL.

View source

#route_params=(route_params : Hash(String, String | Nil))#

The parameters that should be used when constructing the redirect #route URL.

View source

#set_header(name : String, value : String) : Nil#

Adds the provided header name and value to the underlying #response.

View source

#set_header(name : String, value : _) : Nil#

Adds the provided header name and value to the underlying #response.

View source

#status : HTTP::Status | ::Nil#

The HTTP::Status of the underlying #response.

View source

#status=(status : HTTP::Status | Nil)#

The HTTP::Status of the underlying #response.

View source