How can I conditionally wrap some HAML content in a tag?

前端 未结 2 2067

How can I use a condition to decide whether to output a surrounding tag in HAML? I\'m trying to create the DRY version of the code below.

- if i_should_link          


        
2条回答
  •  我在风中等你
    2020-12-03 17:21

    You could use a partial.

    foo.html.haml

    - if i_should_link
      %a{:href => url}
        = render 'bar'
    - else
      = render 'bar'
    

    _bar.html.haml

    .foo
      .block
        .of
          .code
    

    Edit: Or you could use content for, I guess this is better because it keeps it all in the same file.

    - if i_should_link
      %a{:href => url}
        = yield :foobar
    - else
      = yield :foobar
    
    - content_for :foobar do
      .foo
        .block
          .of
            .code
    

提交回复
热议问题