Skip to content

class Athena::Routing::View(T)
inherits Reference #

An ART::View represents an ART::Response, but in a format agnostic way.

Returning a ART::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 ART::Response.

require "athena"

class HelloController < ART::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) : ART::View(NamedTuple(greeting: String))
    self.view({greeting: "Hello #{name}"}, :im_a_teapot)
  end
end

ART.run

# GET /Fred      # => 200 {"greeting":"Hello Fred"}
# GET /view/Fred # => 418 {"greeting":"Hello Fred"}

See the negotiation component for more information.

Included modules

Athena::Routing::ViewBase

Class methods#

.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, _)? = nil, 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, status : HTTP::Status? = nil, headers : HTTP::Headers = HTTP::Headers.new) #

View source

Methods#

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

View source

#data : T #

The response data.

View source

#data=(data : T) #

The response data.

View source

#format : String? #

The format the view should be rendered in.

The format must be registered with the ART::Request::FORMATS hash; either as a built in format, or a custom one that has registered via ART::Request.register_format.

View source

#format=(format : String?) #

The format the view should be rendered in.

The format must be registered with the ART::Request::FORMATS hash; either as a built in format, or a custom one that has registered via ART::Request.register_format.

View source

#headers : ART::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? #

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 : ART::Response #

The wrapped ART::Response instance.

View source

#response=(response : ART::Response) #

The wrapped ART::Response instance.

View source

#return_type : T.class #

Returns the type of the data represented by self.

View source

#route : String? #

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

See the URL Generation documentation.

View source

#route=(route : String) : Nil #

Sets the redirect #route.

View source

#route_params : Hash(String, String)? #

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

View source

#route_params=(route_params : Hash(String, String)?) #

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

The HTTP::Status of the underlying #response.

View source

#status=(status : HTTP::Status?) #

The HTTP::Status of the underlying #response.

View source