<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Meseret Gebre &#187; partials</title>
	<atom:link href="http://meseretgebre.com/archives/tag/partials/feed/" rel="self" type="application/rss+xml" />
	<link>http://meseretgebre.com</link>
	<description></description>
	<lastBuildDate>Wed, 16 Nov 2011 04:42:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails 2.0 Patrials and Collections part2</title>
		<link>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part2/</link>
		<comments>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part2/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 03:40:13 +0000</pubDate>
		<dc:creator>mez</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[partials]]></category>

		<guid isPermaLink="false">http://meseretgebre.com/?p=29</guid>
		<description><![CDATA[In part one of this article we went over how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. We&#8217;ll see how you can set additional local variables in your partial. This article is short and [...]]]></description>
			<content:encoded><![CDATA[<p>In part one of this article we went over how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. We&#8217;ll see how you can set additional local variables in your partial. This article is short and assume you have read part1 of this article.</p>
<p><strong>Outline for this article</strong></p>
<ol>
<li>How do we set local variables in Rails Partials?</li>
<li>Using Rails Partials in a loop?</li>
<li>Is there a point to the Spacer Template in Rails Partials?</li>
</ol>
<p><strong>How do we set local variables in Rails Partials? </strong><br />
Ok, so we know that we can pass in an object to a partial with the</p>
<pre class="brush: python">
 <img src='http://meseretgebre.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject
</pre>
<p>symbol. Great, but what if you want to use more variables in the partial.<br />
This can be done using the</p>
<pre class="brush: python">
:locals
</pre>
<p>symbol in the render helper. For example if go back to the example given in part1</p>
<pre class="brush: python">
&lt;%= render :partial =&gt; &quot;shared/testpartial&quot;, <img src='http://meseretgebre.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject =&gt; @name , :locals =&gt; {:testvar =&gt; @any_object_you_want } %&gt;;
</pre>
<p>Just make sure you know the name of the local variables you are setting.</p>
<p><strong>Using Rails Partials in a loop?</strong><br />
With partials we can format create modular views. Sometimes you&#8217;ll find the need to<br />
render a partial many time over. On way you can do this is to run the partial in a for loop.<br />
Rails has a shortcut for this. You can use the symbol</p>
<pre class="brush: python">
:collection
</pre>
<p>The symbol takes a collection of objects that the partial takes! The example from <a href="http://meseretgebre.com/?p=26">part1</a> took a string object. So to use the collection we can pass it a list of string objects like the following</p>
<pre class="brush: python">
&lt;%= render :partial =&gt; &quot;shared/testpartial&quot;,
:collection =&gt; %w{ Mez Adam Azmara John} %&gt;;
</pre>
<p>This is a very useful feature, if used correctly your views show look clean. This makes maintenance<br />
of code very manageable and enjoyable.</p>
<p><strong>Is there a point to the Spacer Template in Rails Partials?</strong><br />
There is a option the render helper can take called</p>
<pre class="brush: python">
:spacer_template
</pre>
<p>It lets you specify a template that will be rendered between each of the elements in the collection.<br />
Basically, it renders another parital. Here is how to use it.</p>
<pre class="brush: python">
&lt;%= render :partial =&amp;gt; &quot;shared/testpartial&quot;,
:collection =&gt; %w{ Mez Adam Azmara John},
:spacer_template =&gt; &quot;shared/another_partial&quot; %&gt;;
</pre>
<p>Note, this assumes you have a &#8220;_another_partial.html.erb&#8221; handy. My thinking on this is a little off. I am not sure if you really need to define another partial to display in between partials. Why not just define the content of the spacer template in the first partial? Give some good examples if you think of any!</p>
<p>That concludes this article. I hope you enjoyed it!</p>
]]></content:encoded>
			<wfw:commentRss>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails 2.0 Patrials and Collections part1</title>
		<link>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part1/</link>
		<comments>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part1/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 03:29:37 +0000</pubDate>
		<dc:creator>mez</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[partials]]></category>

		<guid isPermaLink="false">http://meseretgebre.com/?p=26</guid>
		<description><![CDATA[Often in web applications, the same data is displayed in more than one location. Usually this is done by copying code between different template pages. Using Rails we can avoid this and remain true to the Rails principles of &#8220;never repeating ourselves&#8221;. This is accomplished by using Rails partials. In part one of this article [...]]]></description>
			<content:encoded><![CDATA[<p>Often in web applications, the same data is displayed in more than one location. Usually this is done by copying code between different template pages. Using Rails we can avoid this and remain true to the Rails principles of &#8220;never repeating ourselves&#8221;. This is accomplished by using Rails partials.</p>
<p>In part one of this article we&#8217;ll see how to create and use partials. Part two will take what we learned from part one and see how we can use collections to make partials even more useful. Note, more info can be found from the book &#8220;Agile Web Development with Rails&#8221; in the chapter that deals with ActionView. </p>
<p><strong>Outline for this article</strong></p>
<ol>
<li> What the heck are Rails Partials?</li>
<li> How do we create Rails Partials?</li>
<li> How to use Rails Partials?</li>
</ol>
<p><strong>What the heck are Rails Partials?</strong><br />
Rails Partials are basically subroutines in their own right. You can use them many times and in many different templates. If you want, you can even pass in objects to render as parameters. A partial template looks just like a regular templates in Rails with the exception of the name of the file containing the partial. The name of the file containing the template code must start with an underscore character.</p>
<p><strong>How do we create a Rails Partials?</strong><br />
I learn best by examples. So that&#8217;s what we&#8217;ll do here! Let take a simple example that serves no purpose but to learn from. Lets create a simple partial template that will take a string object and simply render the string. In a file named &#8220;_testpartial.html.erb&#8221;.</p>
<p>Some background information to keep in mind. you might be wondering where do you store this partial file? Well that depends. If you want to use this partials in views from multiple controllers, then what I like to do is create a folder called &#8220;shared&#8221; in the views directory. So go ahead and do that now. Are you done yet? Ok, lets keep moving.</p>
<pre class="brush: python">
Hello, &lt;%=testpartial%&gt;;
</pre>
<p>Keep in mind that the partial in this example expects a string object. Well now you might be wondering how does the partial know the name of the object being passed in? It uses the name of the partial as a variable name. Since this simple partial is called &#8220;_testpartial.html.erb&#8221;, we can use the variable &#8220;testpartial&#8221;. Now that we have this partial setup and created lets go and use it!</p>
<p><strong>How to use Rails Partials?</strong><br />
Using a partial is really simple. You can do this in two places. The first way is in the controller like the following.</p>
<pre class="brush: python">
def index
@name = &quot;Meseret Gebre&quot;
render :partial =&amp;gt; &quot;shared/testpartial&quot;, <img src='http://meseretgebre.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject =&amp;gt; @name
end
</pre>
<p>The second way is in the view. For example if we setup the instance variable in the controller like the following.</p>
<pre class="brush: python">
def index
#var to be used in view.
@name = &quot;Meseret Gebre&quot;
end
</pre>
<p>Then in the view &#8220;index.rhtml&#8221; we can render the partial by placing the following code somewhere in the view.</p>
<pre class="brush: python">
&lt;%=render :partial =&gt; &quot;shared/testpartial&quot;, <img src='http://meseretgebre.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject =&gt; @name%&gt;;
</pre>
<p>Thats Rails partials in a nutshell. Remember in <a href="http://meseretgebre.com/?p=29">part2</a> of this article, we&#8217;ll look at using partials to render a collections of objects! I hope you learned something cool here and remember to never repeat yourself! Any questions or comments are welcomed!</p>
]]></content:encoded>
			<wfw:commentRss>http://meseretgebre.com/archives/rails-20-patrials-and-collections-part1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

