Using Kemal CSRF handler with Crinja

2018 05 19 No Comments

I assume that you are using kemal, kemal-session and kemal-csrf. Require them and use add_handler to handle requests.

Default input name is authenticity_token so I used it.

require "kemal"
require "kemal-session"
require "kemal-csrf"

  allowed_methods: ["GET", "HEAD", "OPTIONS", "TRACE"],
  error: ->myerrorhandler(HTTP::Server::Context)

def myerrorhandler(env)
  if env.request.headers["Content-Type"]? == "application/json"
    {"error" => "csrf error"}.to_json
    "No token!"

Create a function to use it in templates

Crystal seo friendly url

2018 05 19 No Comments

Maybe someone needs it.

Markdown render in template side with Crinja – Crystal

2018 05 18 No Comments

In crystal ecr templates, we can use render markdown like this: <%= Markdown.to_html("# text") %>

But with crinja we cant do it as I see.

So I created template function.

require "markdown"

c =
c.loader ="src/views/")

c.functions << Crinja.function({:str => ""}, :Markdown) do

To use it:

{{ Markdown("# title")  | safe}}


Crystal code reloading with nodemon

2018 05 14 No Comments

I saw this solution in this link.

First install nodemon globally:
npm i -g nodemon

After install, run your main cr file:
nodemon --exec "crystal run" src/{YOUR_MAIN_FILE}.cr

In my computer (macbook early 2015 8/128), it works faster than sentry.

Ps: Nodemon doesn’t works with ecr templates, sentry works.