2 Replies - 1631 Views - Last Post: 23 July 2011 - 04:32 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

rails - iterating over arrays with partials (first iteration unique)

Posted 22 July 2011 - 01:23 PM

Hey, I recently learned that you can make partials for your 'table-like' HTML elements. This makes it easier to change the contents of your HTML elements, with out having to even look at all that messy HTML code (well, in my case, it's pretty clean, but you know).

So the name of my HTML element is News-Attacks, and it displays:
1) A Bordered HTML division
2) A header reading "NEWS ATTACKS"
3) A division specifying the date "- JULY 20 -"
4) An array of news statements.

http://i1185.photobu...f/newattack.jpg


So here's my code

<% @news_attack_items = [ "I am testing a new UI Element!", "It will iterate over an array of strings!", "all of the strings will be prefixed with a <br> toke, BUT NOT THE FIRST ONE."]%>


<div id="news-attack" style="text-align:center; border-width:thin; border-style:solid; ">
	<div id="nb-title"><p><b>NEWS ATTACK</b></p> </div>
	<div id="nb-date"><p style="display:block; text-align:center; margin:auto;">- July 19 -</p> </div>
	
	<%= render :partial => 'shared/news_attack_item', :collection => @news_attack_items %>  <%# auto iterates since you specify a collection%>
</div>



('shared/news_attack_item')
	<br><p><%=news_attack_item.html_safe %></p>



How can I make it so that on the first iteration (or the last) it doesn't send the '<br>' token?

This post has been edited by NotarySojac: 22 July 2011 - 01:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: rails - iterating over arrays with partials (first iteration unique)

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,266
  • Joined: 21-June 11

Re: rails - iterating over arrays with partials (first iteration unique)

Posted 23 July 2011 - 01:04 AM

The render method accepts an option :spacer_template, which when given will cause another partial to be rendered after each element except the last. So you can create another partial which just includes <br> and then pass its name as the :spacer_template to render.

That being said, I don't see why you need to use both <p> and <br> and why it would be bad to have a <br> at the end of the last line.
Was This Post Helpful? 1
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: rails - iterating over arrays with partials (first iteration unique)

Posted 23 July 2011 - 04:32 PM

Thanks sepp2k, it works perfect.

In my css, I have my paragraphs zeroed so they don't cause breaks.

http://cssdiary.word...ents-on-a-page/

I like it cause I really get into naming the IDs of all of my elements and having full control over their appearance. Also, ppl say it makes cross browser stuff more 1:1 due to the way different browsers use margin rather than padding and vice verse.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1