<?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>Sean&#039;s Blog &#187; erb</title>
	<atom:link href="http://seanbehan.com/tag/erb/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +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>Build Your Own Calendar in Rails without any Plugins in less than 10 lines of Ruby Code</title>
		<link>http://seanbehan.com/ruby-on-rails/build-your-own-calendar-in-rails-without-any-plugins-in-less-than-10-lines-of-ruby-code/</link>
		<comments>http://seanbehan.com/ruby-on-rails/build-your-own-calendar-in-rails-without-any-plugins-in-less-than-10-lines-of-ruby-code/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 17:08:12 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=553</guid>
		<description><![CDATA[There are a number of terrific calendar plugins for Rails. But it&#8217;s almost as easy, if not easier, to implement your own calendar. The steps are pretty simple. First get the @beginning_of_month and @end_of_month and iterate over the days in between. In the loop we check if the day matches the @beginning_of_month and if it [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fbuild-your-own-calendar-in-rails-without-any-plugins-in-less-than-10-lines-of-ruby-code%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Build Your Own Calendar in Rails without any Plugins in less than 10 lines of Ruby Code" data-url="http://seanbehan.com/ruby-on-rails/build-your-own-calendar-in-rails-without-any-plugins-in-less-than-10-lines-of-ruby-code/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>There are a number of terrific calendar plugins for Rails. But it&#8217;s almost as easy, if not easier, to implement your own calendar.</p>
<p>The steps are pretty simple. First get the @beginning_of_month and @end_of_month and iterate over the days in between. In the loop we check if the day matches the @beginning_of_month and if it does we get the offset ( 0 &#8211; 6 ) because if the month doesn&#8217;t start on a Sunday all our days won&#8217;t match up correctly. We print out the offset number as table cells &lt;td class=&#8217;offset&#8217;&gt;&lt;/td&gt;. We then need to consider that weeks &#8216;restart&#8217; and restart our row if we&#8217;re at the beginning of the week&#8230; this is accomplished with &lt;/tr&gt;&lt;tr&gt;. Then we just output the date contained in table cells &lt;td&gt;#{d.day}&lt;/td&gt;.  The code is below and it&#8217;s pretty simple.</p>
<p>In this example I use haml, but you could just as easily use ERB (at the bottom). If you&#8217;re not familiar with haml, check it out at http://haml-lang.com/ Haml automatically handles wrapping your html so you don&#8217;t have to! The end result is that it cuts your html in half and makes it look pretty, which in turns makes it a lot more manageable long term.</p>
<p>#app/views/calendars/show.html.haml</p>
<pre class="wp-code-highlight prettyprint">%table#calendar
  %tr
    %th
      Sunday
    %th
      Monday
    %th
      Tuesday
    %th
      Wednesday
    %th
      Thursday
    %th
      Friday
    %th
      Saturday
  %tr
    - @beginning_of_month = Date.civil(2009,12,1)
    - @end_of_month       = Date.civil(2009, 12, -1)

    - (@beginning_of_month..@end_of_month).each do |d|
      - if d == @beginning_of_month
        - (d.wday).times do # offset beginning of calendar
         &amp;lt; td class='offset'&amp;gt; &amp;lt;/td&amp;gt;
      -if d.wday == 0 #restart the week
        &amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt;

      == &amp;lt;td class='#{d}'&amp;gt; #{d.day} &amp;lt;/td&amp;gt;</pre>
<p>Here it is in ERB</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;&lt;span&gt;table&lt;/span&gt;&lt;span&gt; id&lt;/span&gt;=&lt;span&gt;'calendar'&lt;/span&gt;&amp;gt;
 &amp;lt;&lt;span&gt;tr&lt;/span&gt;&amp;gt;
   &amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Sunday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;&amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Monday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;
   &amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Tuesday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;&amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Wednesday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;
   &amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Thursday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;&amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Friday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;
   &amp;lt;&lt;span&gt;th&lt;/span&gt;&amp;gt;Saturday&amp;lt;/&lt;span&gt;th&lt;/span&gt;&amp;gt;
  &amp;lt;/&lt;span&gt;tr&lt;/span&gt;&amp;gt;
  &amp;lt;&lt;span&gt;tr&lt;/span&gt;&amp;gt;
  &amp;lt;% @beginning_of_month = Date.civil(2009, 12, 1) %&amp;gt;
  &amp;lt;% @end_of_month = Date.civil(2009, 12, -1) %&amp;gt;

  &amp;lt;% (@beginning_of_month..@end_of_month).each do |d| %&amp;gt;
    &amp;lt;% if d == @beginning_of_month %&amp;gt;
      &amp;lt;% d.wday.times do %&amp;gt; &amp;lt;td class='offset'&amp;gt;&amp;lt;/td&amp;gt; &amp;lt;% end %&amp;gt;
    &amp;lt;% end %&amp;gt;

    &amp;lt;% if d.wday == 0 %&amp;gt; &amp;lt;/tr&amp;gt;&amp;lt;tr&amp;gt; &amp;lt;% end %&amp;gt;
    &amp;lt;td&amp;gt; &amp;lt;%= d.day %&amp;gt; &amp;lt;/td&amp;gt;
  &amp;lt;% end %&amp;gt;
  &amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/build-your-own-calendar-in-rails-without-any-plugins-in-less-than-10-lines-of-ruby-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

