HAML: Create container/wrapper element only if condition is true

前端 未结 4 692
礼貌的吻别
礼貌的吻别 2021-01-02 04:01

Longshot, but I\'m wondering if there\'s any way to do something like this:

%p # ONLY SHOW THIS IF LOCAL VARIABLE show_paras IS TRUE
  = name
4条回答
  •  生来不讨喜
    2021-01-02 04:17

    You could use raw html, but then you'd have to have the if statement both at the beginning and end:

    - if show_paras
      

    = name - if show_paras

    Assuming you're doing more than just = name, you could use a partial:

    - if show_paras
      %p= render "my_partial"
    - else
      = render "my_partial"
    

    You could also use HAML's surround (though this is a little messy):

    - surround(show_paras ? "

    " : "", show_paras ? "

    " : "") do = name

    Finally, what I would probably do is not try to omit the p tag at all, and just use CSS classes to set up two different p styles to look the way I want:

    %p{:class => show_paras ? "with_paras" : "without_paras"}
      = name
    

提交回复
热议问题