How to Conditionally Wrap Some Haml Content in a Tag

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

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

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

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

- if show_paras
<p>
= name
- if show_paras
</p>

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 ? "<p>" : "", show_paras ? "</p>" : "") 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

Conditionally closing a tag in HAML

The answer is to use more and better Ruby :)

- @items.group_by(&:date).each do |date,items|
.container
%h2= date
- items.each do |item|
= yield_content :display_item item

See Enumerable#group_by docs for more details.

You could close and re-open the containers and headers as you were thinking, but this is a horrible, hard-to-maintain hack that I would suggest against. Embrace the elegance of Ruby and Haml; don't write Haml like it was ERB.

HAML iterate over collection - how to wrap groups of child elements in unique parents?

Try to think in terms of organising your data before writing it out to HTML. In this case you could use group_by on the collection first, then iterate over each array in turn, something like:

- foo.group_by {|m| m['type']}.each do |type, array|
%h2= type.capitalize
%ul{:class => "list-#{type}"}
-array.each do |m|
%li= m['title']

(Here I’ve just used capitalize to create the headers, you might need something more complex depending on what you want.)

How to remove unwanted indent from HAML's pre tag

You need to use the #preserve helper to convert the newlines in the pre to newline entities, like so:

%pre.code
= preserve do
:escaped
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
<form>
<input type="text" name="empID" />
<input type="submit"/>
</form>
</body>
</html>

In the future, you'll be able to nest filters, so you can do :preserve:escaped.

How to do an if/else in HAML without repeating indented code

!!!
- @user = "jed" #=> stubbing out an instance
%html
%head
- string = defined?(@user) ? "#{@user}" : nil #=> for demo only, wrap this in a helper method
%title{'data-account' => string}
%body
=yield


Related Topics



Leave a reply



Submit