luckyframework / lucky

A full-featured Crystal web framework that catches bugs for you, runs incredibly fast, and helps you write code that lasts.
https://luckyframework.org
MIT License
2.57k stars 156 forks source link

Add native ECR support #1858

Open jwoertink opened 3 months ago

jwoertink commented 3 months ago

We have docs on setting up Kilt and using ECR embedded in your page https://luckyframework.org/guides/frontend/rendering-html#rendering-html-with-templates-ecr-slang-etc

But this setup is a bit complicated since it relies on you setting up a full Lucky::Page then embedding your ECR within it.

You can also use ECR directly from your action, but it's not as straight forward, and not documented:

class Posts::Index < BrowserAction
  get "/posts" do
    posts = PostQuery.new
    send_text_response ECR.render("src/pages/posts.ecr"), content_type: html_content_type, status: 200
  end
end

We should just make a method like

# assume these are always in `./src/pages/`
# Also default assume content_type is HTML, but allow for an override
render_ecr "posts/index.ecr"

def render_ecr(page_path : String, content_type : String = html_content_type, status : Int32? = nil) : Lucky::TextResponse
  send_text_response ECR.render(Path["#{Dir.current}/src/pages/#{page_path}"]), 
    content_type: content_type,
    status: status
end

Only wonky thing I'm not a fan of is that variables have to be defined in this weird floating area. You'd have to tell Ameba to ignore these because they'd be used in the template. But this would be fairly close to how Kemal works https://kemalcr.com/guide/#views-templates