SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2005
    Location
    Silver Spring MD, USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Views and attributes

    Okay, I'll start by saying that I'm really new at RoR, but I haven't been able to find an answer to this anywhere.

    I have a blog that outputs the 5 most recents posts on the main page and each can be expanded/collapsed. When the page is first output, only the first post's div gets a different class attribute than the remaining four, but I can't seem to find out how to print that to the template. In PHP, I would do something like:
    Code:
    for (i=0;i<5;i++){
    if (i==0){print (<div class='show'></div>}
    else{print(div class='hide'></div>}
    }
    But now, I'm trying to keep as much of that kind of functionality as I can in helpers. Is there some sort of way I can define something in a helper and just put
    Code:
    <div <% :class => show_hide%> >
    or something similar into the template?

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd be interested to hear people's opinions on this, I don't know the best answer.

    You could certainly use a helper to keep that logic off the template, but it would need to know which post is first. I can think of three ways that could happen.

    1. The post knows that it is first, you test for it with a method you placed in the model ... post.first?, post.most_recent?, something like that.
    2. The helper sets an @instance_variable when it is called, every subsequent call is considered not the first.
    3. The view passes that information along to the helper.

    Three seems like the worst to me, because by the time you've added the code to make the view get the message across to the helper, you may as well skip the helper.
    Two is probably the easiest but violates some sort of programming maxim, I think.
    So I think that one is the way to go. But that also seems tricky.

    The easiest thing may be to separate the first post and don't deal with it in the same loop as the other posts. Could open up some additional possibilities.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Location
    Silver Spring MD, USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your first idea is something like what I was aiming for, actually. What I haven't been able to find is if .first? is a valid method to attach the array. If .first? isn't a pre-built method for Ruby(onRails), then I'm wondering if there's a way to write the expression anything like this:

    In the controller, I would first declare
    Code:
    def list
    @posts = Post.find_all
    end
    In the main page code, I would iterate through the first 3 using
    Code:
    <% @posts.values_at(0..2).each do |post| %>
    The thing that stumps me is if I can set a helper function that would either say
    Code:
    if posts.first?
    divclass = 'show'
    #or
    if posts.index=0
    divclass = 'show'
    Like I said, I know that PHP can keep track of exactly what index/key in an array is being currently iterated through, but I'm not sure if RoR has that same functionality.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <% @posts.each_with_index do |post, position| -%>
       <div class="<%= (position == 0) ? 'show' : 'hide' %>">
          <%= post.content %>
       </div>
    <% end -%>

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    Code:
    <% @posts.each_with_index do |post, position| -%>
       <div class="<%= (position == 0) ? 'show' : 'hide' %>">
          <%= post.content %>
       </div>
    <% end -%>
    each_with_index! Hot damn, I didn't know that existed.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •