How to pass an argument when calling a view file?

后端 未结 4 654
深忆病人
深忆病人 2021-02-04 01:38

I wrote a webform using Sinatra and Haml that will be used to call a Ruby script.

Everything seems fine except for one thing: I need to pass an argument to a Haml view f

相关标签:
4条回答
  • 2021-02-04 01:43

    Given

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

    I'd try

    haml :success, {}, {my_var: my_value}
    
    0 讨论(0)
  • 2021-02-04 01:53

    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}

    0 讨论(0)
  • 2021-02-04 02:03

    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.

    0 讨论(0)
  • 2021-02-04 02:03

    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.

    0 讨论(0)
提交回复
热议问题