要有条件地包含标签元素,请使用定义?诀窍在于三元组 - 将属性值设置为 nil 将删除它。( https://gist.github.com/orioltf/3145400 )
举个例子(尝试用一些快速的 ruby 代码来模拟你的情况)
- _h1 = {'title' => 'Title', 'url' => "http://example.com", 'image_url'=> "img.png", 'attributes'=> {'target'=> '_blank'}}
- _h2 = {'title' => 'Title2', 'url' => "http://example2.com", 'image_url'=> "img2.png"}
- $data_list = [_h1, _h2]
- class Model; def data() $data_list end end
- @model = Model.new
- for item in @model.data
%a.menu-item{:href => item['url'],
:target => (defined? item['attributes']['target']) ? item['attributes']['target'] : nil}
请注意,我使用的是哈希访问器。根据您设置的对象、您使用的框架、您可以使用我所做的、item.url、item.get('url') 等。
您可以使用 haml test.haml test.html 对其进行测试,希望可以帮助您入门。