开发者

Ruby on Rails, how to structure this HAML code to not include some <div> at top level?

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?


It sounds to me like you do indeed want a partial - the fact there's no good way to do it with this hierarchy is a good sign to me that you want a partial. Then you can use it elsewhere, easily render a collection, or just generally partition that code.

Also, if this need occurs in other places in your project, you might want to consider using different default layouts for your different response formats.

However, you might also consider this pattern:

%div{:class => expr ? 'one' : 'two'}

which will vary the class based on expr. You would still end up with extra divs, but they could be of some other custom class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜