Search code examples
htmlcrystal-langkemal

How do you pass local variables to views in Kemal?


Edit: Link for documentation

I'm new to Crystal and I'm trying to build a small web-app with Kemal framework.

I have some experience working with Ruby and it's frameworks (Rails and Sinatra). In Rails/Sinatra you can pass local variables to views that you are about to render. Something like this:

render(:some_view, locals: { foo: :bar })

Variable foo with value bar will become available in view.

So I thought that the same goes here, but I can't find anything like that in Kemal guide or their GitHub page nor in their Cookbook pages.

What am I missing here? Maybe there is some other completely different way of doing this in Kemal that I'm not aware of?


Solution

  • Define variable in the controller

    get "/" do
      name = "Sergey"
      render "src/views/main.ecr"
    end
    

    Use it in the view

    <body>
      My name is <%= name %>
    </body>