【问题标题】:How to pass a hash object to a HAML tag如何将哈希对象传递给 HAML 标签
【发布时间】:2012-09-04 07:57:46
【问题描述】:

请考虑这个例子:

- user_links_params = _user_link_params(current_user)

%a{ :'data-msgstore-path' => user_links_params[:'data-msgstore-path'],
  :'data-user_id' => user_links_params[:'data-user_id'],
  :class => user_links_params[:class],
}
  / too many html tags and stuff to fit in a simple link_to

我会很高兴将所有这些都放在一个简单的语句中,如下所示:

%a[_user_link_params(current_user)]
  / too many html tags and stuff to fit in a simple link_to

【问题讨论】:

    标签: ruby haml


    【解决方案1】:

    是的,有可能,而且你很接近:

    %a{_user_link_params(current_user)}
    

    来自HAML reference

    例如,如果你定义了

    def hash1
      {:bread => 'white', :filling => 'peanut butter and jelly'}
    end
    
    def hash2
      {:bread => 'whole wheat'}
    end
    

    然后%sandwich{hash1, hash2, :delicious => true}/ 将编译为:

    <sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' />
    

    【讨论】:

    • 小警告,方法需要返回一个hash,不能返回nil
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多