Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Conditional Nesting in HAML?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Cataclyst  
View profile  
 More options Jun 29, 9:33 am
From: Cataclyst <a.lohr...@gmx.net>
Date: Mon, 29 Jun 2009 06:33:53 -0700 (PDT)
Local: Mon, Jun 29 2009 9:33 am
Subject: Conditional Nesting in HAML?
Hi all,

so far I've been using haml with joy, but now I'm having a problem I'm
not able to solve: I want to nest content within a div, but only if a
specific local variable is true. The idea is to render a partial with
or without the surrounding "container" div just as needed, in order to
render a new item block or just replace the content within it (as an
AJAX response).

In ERB, I would do something similar to the following:

######

<% if !skip_outer -%>
  <div id="some_id">
<% end -%>

 <!-- some content here -->

<% if !skip_outer -%>
  </div>
<% end -%>

#######

How do I accomplish the same thing with HAML? If-then statements don't
really work:

- if !skip_outer
  #some_id
  / content here would be rendered only on !skip_outer, and even not
within the div#some_id

neither does it with an else case:

- if !skip_outer
  #some_id
- else
  / content here would be rendered only on skip_outer

Any ideas to get something like that work "gracefully", i.e. without
having to double the content in both if-else-branches?

Thanks in advance.
André


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
scottwb  
View profile  
(1 user)  More options Jun 29, 12:50 pm
From: scottwb <scot...@gmail.com>
Date: Mon, 29 Jun 2009 09:50:14 -0700 (PDT)
Local: Mon, Jun 29 2009 12:50 pm
Subject: Re: Conditional Nesting in HAML?
Try something like this...define a helper like:

def conditional_div(condition, attributes, &block)
  if condition
    haml_tag :div, attributes, &block
  else
    haml_concat capture_haml(&block)
  end
end

Then, in your template do:

- conditional_div(!skip_outer, {:id => "some_id"}) do
  / some content here

On Jun 29, 6:33 am, Cataclyst <a.lohr...@gmx.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cataclyst  
View profile  
 More options Jun 29, 2:28 pm
From: Cataclyst <a.lohr...@gmx.net>
Date: Mon, 29 Jun 2009 11:28:48 -0700 (PDT)
Local: Mon, Jun 29 2009 2:28 pm
Subject: Re: Conditional Nesting in HAML?
That's it, the helper method works great. :) Thanks a lot, Scott.

André

On 29 Jun., 18:50, scottwb <scot...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan Weizenbaum  
View profile  
 More options Jun 29, 10:31 pm
From: Nathan Weizenbaum <nex...@gmail.com>
Date: Mon, 29 Jun 2009 19:31:34 -0700
Local: Mon, Jun 29 2009 10:31 pm
Subject: Re: [haml] Re: Conditional Nesting in HAML?

Or more directly,

def haml_tag_if(condition, *args, &block)
  if condition
    haml_tag *args, &block
  else
    yield
  end
end

That might be something worth adding to Haml::Helpers. Thoughts?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Noel  
View profile  
 More options Jun 30, 9:05 pm
From: Noel <wwydi...@gmail.com>
Date: Tue, 30 Jun 2009 18:05:38 -0700
Local: Tues, Jun 30 2009 9:05 pm
Subject: Re: [haml] Re: Conditional Nesting in HAML?
I have a somewhat related question

Could I use this technique to conditionally nest some content?  I'm
sorry if this is obvious from the last response, but I didn't get it
yet.

I want to render some columns and in the controller I determine how
many items should be in each column.

the markup I am trying to get is someting like

%div#col_0
  ul.group
    li.item
    li.item
    li.item
  ul.group
    li.item
    li.item
%div#col_1
  ul.group
    li.item
  ul.group
    li.item
    li.item

from index.html.haml I am calling render :partial on group and from
that partial I render :partial item

So I need to be able to conditionally start / end the column div in
the group partial.

How should I go about this?

Thanks,
Noel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan Weizenbaum  
View profile  
 More options Jul 1, 12:11 am
From: Nathan Weizenbaum <nex...@gmail.com>
Date: Tue, 30 Jun 2009 21:11:56 -0700
Local: Wed, Jul 1 2009 12:11 am
Subject: Re: [haml] Re: Conditional Nesting in HAML?

I'd use a helper that takes the array of items and divides it up into
sub-arrays of the desired length (maybe use Array#each_slice).


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google