# How to structure this HAML code to not include some \<div\>

**URL:** <https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437>\
**Category:** Rails\
**Created:** [July 30, 2010, 10:56am UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437 "2010-07-30T10:56:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [July 30, 2010, 10:56am UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437/1 "2010-07-30T10:56:30Z")

</div>

if the HAML is:

```
.class1
  .class2
    .class3
      %div
        content... many lines

```

How can it be made so that it doesn’t respond with class1 and class2 if  
it is Ajax?

```
- if !request.xhr?
  .class1
    .class2

.class3
  %div
    content... many lines

```

won’t work, because if not Ajax, then class3 is not a child of class2.  
It might have to be something like

```
- if !request.xhr?
  .class1
    .class2
      .class3
        %div
          content... many lines
- else
  .class3
    %div
      content... many lines

```

and it is repeating a lot of code. Can it be structure within the same  
file? Or the second part made into a partial?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [July 30, 2010, 5:37pm UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437/2 "2010-07-30T17:37:23Z")

</div>

winter heat wrote:

> if the HAML is:
> 
> ```
> .class1
> .class2
> .class3
> %div
> content... many lines
> 
> ```
> 
> How can it be made so that it doesn’t respond with class1 and class2 if  
> it is Ajax?

Well, it’s not quite clear what you really want.

First of all, do you need to nest the divs at all? Remember that one  
element can have multiple classes, so .class1.class2.class3 is possible.

If not, then can you extract the .class1 and .class2 into a layout?

> ```
> - if !request.xhr?
> .class1
> .class2
> 
> .class3
> %div
> content... many lines
> 
> ```
> 
> won’t work, because if not Ajax, then class3 is not a child of class2.  
> It might have to be something like
> 
> ```
> - if !request.xhr?
> .class1
> .class2
> .class3
> %div
> content... many lines
> - else
> .class3
> %div
> content... many lines
> 
> ```
> 
> and it is repeating a lot of code. Can it be structure within the same  
> file?

Perhaps, but your view shouldn’t be testing request.xhr? . That’s a  
terrible idea.

> Or the second part made into a partial?

That’s probably a better idea.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [July 30, 2010, 7:10pm UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437/3 "2010-07-30T19:10:48Z")

</div>

let’s assume class1, 2, and 3, etc all have their functions, so we need  
all of them.

the 3rd code piece is what we want: class 3 should be nested inside of  
class2, which is what this question is all about.

why is using “request.xhr?” inside of a view not a good idea? if so,  
you should only use it in controller and why?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [July 30, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437/4 "2010-07-30T21:06:22Z")

</div>

[Please quote when replying. It makes the discussion easier to follow.]

winter heat wrote:

> let’s assume class1, 2, and 3, etc all have their functions, so we need  
> all of them.

But do you need them nested, or can they be on the same element?

> the 3rd code piece is what we want: class 3 should be nested inside of  
> class2, which is what this question is all about.
> 
> why is using “request.xhr?” inside of a view not a good idea?

Because the view shouldn’t know anything about the nature of the  
request. The view should only know what the controller tells it.

> if so,  
> you should only use it in controller and why?

Because according to Rails MVC design principles, views should be as  
dumb as possible. The request object is really none of the view’s  
business – all the view should do is display the data the controller  
passes it.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [July 30, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/how-to-structure-this-haml-code-to-not-include-some-div/192437/5 "2010-07-30T21:37:22Z")

</div>

Marnen Laibow-Koser wrote:

> [Please quote when replying. It makes the discussion easier to follow.]
> 
> winter heat wrote:
> 
> > let’s assume class1, 2, and 3, etc all have their functions, so we need  
> > all of them.
> 
> But do you need them nested, or can they be on the same element?

yes, they need to be nested, which is why it is a problem with the “if”  
in the first place.
