How to pass an argument when calling a view file?

耗尽温柔 提交于 2019-12-02 20:29:01
grefab

You can pass a hash of parameters to the Haml method using the :locals key:

get '/' do
    haml :index, :locals => {:some_object => some_object}
end

This way the Ruby code in your Haml file can access some_object and render whatever content is in there, call methods etc.

Haml supports passing variables as locals. With Sinatra, you can send these locals like so:

haml :fail, :locals => {:vm_name => name}

and in the view, reference the variable using locals[:vm_name] or simply vm_name.

I'm doing this in Sinatra+Markaby, I think it should be the same with Haml:

In Ruby script: @var = 'foo'

In template: User name: #{@var}

Given

haml(template, options = {}, locals = {})

I'd try

haml :success, {}, {my_var: my_value}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!