module Lucky::Renderable

Direct including types

Defined in:

lucky/renderable.cr

Macro Summary

Instance Method Summary

Macro Detail

macro disable_cookies #

Disable cookies

When disable_cookies is used, no Set-Cookie header will be written to the response.

class Events::Show < ApiAction
  disable_cookies

  get "/events/:id" do
    ...
  end
end

[View source]
macro html(page_class = nil, _with_status_code = 200, **assigns) #

Render a page and pass it data

html is used to pass data to a page and render it. Each key/value pair must match up with each needs declarations for that page. For example, if we have a page like this:

class Users::IndexPage < MainLayout
  needs users : UserQuery

  def content
    @users.each do |user|
      # ...
    end
  end
end

Our action must pass a users key to the html method like this:

class Users::Index < BrowserAction
  get "/users" do
    html IndexPage, users: UserQuery.new
  end
end

Note also that each piece of data is merged with any expose declarations:

class Users::Index < BrowserAction
  expose current_user

  get "/users" do
    # Users::IndexPage receives users AND current_user
    html IndexPage users: UserQuery.new
  end

  private def current_user
    # ...
  end
end

[View source]
macro html_with_status(page_class, status, **assigns) #

Render an HTMLPage with a status other than 200

The status can either be a Number, a HTTP::Status, or a Symbol that corresponds to the HTTP::Status.

class SecretAgents::Index < BrowserAction
  get "/shhhh" do
    html_with_status IndexPage, 472, message: "This page can only be seen with special goggles"
  end
end

See Crystal's HTTP::Status enum for more available http status codes.


[View source]

Instance Method Detail

def component(comp : Lucky::BaseComponent.class, status : Int32 | Nil = nil, **named_args) : Lucky::TextResponse #

Render a Component as an HTML response.

get "/foo" do
  component MyComponent, with: :args
end

[View source]
def component(comp : Lucky::BaseComponent.class, status : HTTP::Status, **named_args) : Lucky::TextResponse #

[View source]
def data(data : String, content_type : String = "application/octet-stream", disposition : String = "attachment", filename : String | Nil = nil, status : Int32 | Nil = nil) : Lucky::DataResponse #

[View source]
def file(path : String, content_type : String | Nil = nil, disposition : String = "attachment", filename : String | Nil = nil, status : Int32 | Nil = nil) : Lucky::FileResponse #

[View source]
def file(path : String, content_type : String | Nil = nil, disposition : String = "attachment", filename : String | Nil = nil, status : HTTP::Status = HTTP::Status::OK) : Lucky::FileResponse #

[View source]
def head(status : Int32) : Lucky::TextResponse #

[View source]
def head(status : HTTP::Status) : Lucky::TextResponse #

[View source]
def html_content_type #

The default global content-type header for HTML


[View source]
def json(body, status : Int32 | Nil = nil, content_type : String = json_content_type) : Lucky::TextResponse #

[View source]
def json(body, status : HTTP::Status, content_type : String = json_content_type) : Lucky::TextResponse #

[View source]
def json_content_type #

The default global content-type header for JSON


[View source]
def plain_content_type #

The default global content-type header for Plain text


[View source]
def plain_text(body : String, status : Int32 | Nil = nil, content_type : String = plain_content_type) : Lucky::TextResponse #

[View source]
def plain_text(body : String, status : HTTP::Status, content_type : String = plain_content_type) : Lucky::TextResponse #

[View source]
def raw_json(body : String, status : Int32 | Nil = nil, content_type : String = json_content_type) : Lucky::TextResponse #

allows json-compatible string to be returned directly


[View source]
def raw_json(body : String, status : HTTP::Status, content_type : String = json_content_type) : Lucky::TextResponse #

[View source]
def send_text_response(body : String, content_type : String, status : Int32 | Nil = nil) : Lucky::TextResponse #

[View source]
def xml(body : String, status : Int32 | Nil = nil, content_type : String = xml_content_type) : Lucky::TextResponse #

[View source]
def xml(body, status : HTTP::Status, content_type : String = xml_content_type) : Lucky::TextResponse #

[View source]
def xml_content_type #

The default global content-type header for XML


[View source]