Responding

witty quip here...

Views and Templates

As of now, Raze has rudimentary view support via Crystal's native Embedded CR (ECR).

Use render to render an ECR view:

get "/user/:user_id" do
  render("views/user_profile.ecr")
end

Halting

Raze provides a few extensions to the context for quick responding.

Halting will close the response and not allow any additional headers to be set, which might affect any middlewares running afterwards. However, since [no middlewares can come after blocks](/docs/routing#route-blocks-are-the-end-of-the-line), it is safe to use halt in a block as opposed to middlewares.

Halt

get "/yee" do |ctx|
  ctx.halt "yee, boi!", 200
  "hello" # this string is not added to the response
end

get "/uh-oh" do |ctx|
  ctx.halt "damn, boi!", 400
end

get "/good" do |ctx|
  # The default status code is 200
  ctx.halt "ahh yeah, boi!"
end

Halt JSON

Same as halt but sets the Content-Type to application/json.

get "/yee" do |ctx|
  ctx.halt_json %({"yee": "boi"})
end

Halt HTML

Same as halt but sets the Content-Type to text/html.

get "/yee" do |ctx|
  html = %(
    <h1 style="font-family:sans-serif;">Hey, giiiirl.</h1>
  )
  ctx.halt_html html
end

Halt Plain

Same as halt but sets the Content-Type to text/plain.

get "/yee" do |ctx|
  ctx.halt_plain %(Hey, giiiirl)
end