<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Shivsingh&#039;s Weblog</title>
	<atom:link href="http://shivsingh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://shivsingh.wordpress.com</link>
	<description>ruby on rails blog</description>
	<lastBuildDate>Sat, 31 Dec 2011 12:39:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='shivsingh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Shivsingh&#039;s Weblog</title>
		<link>http://shivsingh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://shivsingh.wordpress.com/osd.xml" title="Shivsingh&#039;s Weblog" />
	<atom:link rel='hub' href='http://shivsingh.wordpress.com/?pushpress=hub'/>
		<item>
		<title>find distance between two points</title>
		<link>http://shivsingh.wordpress.com/2011/12/31/find-distance-between-to-points/</link>
		<comments>http://shivsingh.wordpress.com/2011/12/31/find-distance-between-to-points/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 12:23:20 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[web 2.0 news]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[distance between points]]></category>
		<category><![CDATA[degree to lat long]]></category>
		<category><![CDATA[lat]]></category>
		<category><![CDATA[long]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>
		<category><![CDATA[mysql distance]]></category>
		<category><![CDATA[distance between lat long]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=148</guid>
		<description><![CDATA[Web has changed a lot in last few years. Few years ago, distance calculation was needed only in aeronautic science. &#8230;<p><a href="http://shivsingh.wordpress.com/2011/12/31/find-distance-between-to-points/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=148&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Web has changed a lot in last few years. Few years ago, distance calculation was needed only in aeronautic science. But that is now required in normal web sites.  So I thought I would write down steps to find out distance between 2 points in simple words here. Hope this could be helpful for someone. And if not, at-least this post can be a reference for myself.</p>
<p>Here is a sample workaround to Find distance between <em><strong>Avinguda Gabriel Roca, 54, 07015 Palma (Mallorca), Spain</strong></em>  AND <em><strong>Avenida de Juan Miró, 174, 07015 Palma, Spain</strong></em></p>
<p>HERE WE GO -</p>
<p><strong>Requirement:</strong> <span style="text-decoration:underline;">Find distance between <strong>distance_from</strong>  and <strong>distance_till</strong></span></p>
<p><strong> distance_from</strong> =  39°33&#8217;1.89&#8243;N,   2°37&#8217;25.21&#8243;E (Avinguda Gabriel Roca, 54, 07015 Palma (Mallorca), Spain)<br />
<strong>distance_till</strong> =  39°33&#8217;12.79&#8243;N,    2°37&#8217;21.44&#8243;E (Avenida de Juan Miró, 174, 07015 Palma, Spain)</p>
<p><strong>Solution:</strong><br />
<em>As per Annexure-I</em> lat and long will be +ve number<br />
distance_from = 39+(33/60.0)+(1.89/3600.0), 2+(37/60.0)+(25.21/3600.0)<br />
distance_till = 39+(33/60.0)+(12.79/3600.0),  2+(37/60.0)+(21.44/3600.0)</p>
<p><em>As per Annexure-II</em><br />
lat1 = 39.550525<br />
lon1 = 2.623669<br />
lat2 = 39.553553<br />
lon2 = 2.622622</p>
<p><em>As per Annexure-III</em> (result of this formula is in KM)<br />
SELECT (6371 * ACOS(SIN(RADIANS( 39.550525 )) * SIN(RADIANS( 39.553553 )) + COS(RADIANS( 39.550525 )) * COS(RADIANS( 39.553553 )) * COS(RADIANS( 2.622622 ) &#8211; RADIANS( 2.623669 )))); <strong>=&gt; 0.348458973417713 KM</strong></p>
<p>Thats it ! Enjoy !</p>
<p><strong>Annexure-I<br />
</strong>POS. LATITUDE = NORTH<br />
NEG. LATITUDE = SOUTH<br />
POS. LONGITUDE = EAST<br />
NEG. LONGITUDE = WEST<br />
i.e NORTH / EAST =&gt; +ve &amp; SOUTH/WEST =&gt; -ve</p>
<p><strong>Annexure-II<br />
</strong>Decimal value = Degrees + (Minutes/60) + (Seconds/3600)</p>
<p><strong>Annexure-III<br />
</strong>select (6371 * ACOS(SIN(RADIANS( %lat1% )) * SIN(RADIANS( %lat2% )) + COS(RADIANS( %lat1% )) * COS(RADIANS( %lat2% )) * COS(RADIANS( %lon2% ) &#8211; RADIANS( %lon1% )))) as distance_in_km;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=148&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2011/12/31/find-distance-between-to-points/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>InnoDB vs MyISAM</title>
		<link>http://shivsingh.wordpress.com/2011/12/24/innodb-vs-myisam/</link>
		<comments>http://shivsingh.wordpress.com/2011/12/24/innodb-vs-myisam/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 03:09:54 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[big table]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[innodb vs myisam]]></category>
		<category><![CDATA[myisam]]></category>
		<category><![CDATA[myisam vs innodb]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[recovery]]></category>
		<category><![CDATA[repair]]></category>
		<category><![CDATA[row lock]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[table lock]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=139</guid>
		<description><![CDATA[InnoDB is newer while MyISAM is older. InnoDB is more complex while MyISAM is simpler. InnoDB is more strict in &#8230;<p><a href="http://shivsingh.wordpress.com/2011/12/24/innodb-vs-myisam/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=139&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<ol>
<li>InnoDB is newer while MyISAM is older.</li>
<li>InnoDB is more complex while MyISAM is simpler.</li>
<li>InnoDB is more strict in data integrity while MyISAM is loose.</li>
<li>InnoDB implements row-level lock for inserting and updating while MyISAM implements table-level lock.</li>
<li>InnoDB has transactions while MyISAM does not.</li>
<li>InnoDB has foreign keys and relationship contraints while MyISAM does not.</li>
<li>InnoDB has better crash recovery while MyISAM is poor at recovering data integrity at system crashes.</li>
<li>MyISAM has full-text search index while InnoDB has not.</li>
</ol>
<p>In light of these differences, InnoDB and MyISAM have their unique advantages and disadvantages against each other. They each are more suitable in some scenarios than the other.</p>
<p>Advantages of InnoDB</p>
<ol>
<li>InnoDB should be used where data integrity comes a priority because it inherently takes care of them by the help of relationship constraints and transactions.</li>
<li>Faster in write-intensive (inserts, updates) tables because it utilizes row-level locking and only hold up changes to the same row that’s being inserted or updated.</li>
</ol>
<p>Disadvantages of InnoDB</p>
<ol>
<li>Because InnoDB has to take care of the different relationships between tables, database administrator and scheme creators have to take more time in designing the data models which are more complex than those of MyISAM.</li>
<li>Consumes more system resources such as RAM. As a matter of fact, it is recommended by many that InnoDB engine be turned off if there’s no substantial need for it after installation of MySQL.</li>
<li>No full-text indexing.</li>
</ol>
<p>Advantages of MyISAM</p>
<ol>
<li>Simpler to design and create, thus better for beginners. No worries about the foreign relationships between tables.</li>
<li>Faster than InnoDB on the whole as a result of the simpler structure thus much less costs of server resources.</li>
<li>Full-text indexing.</li>
<li>Especially good for read-intensive (select) tables.</li>
</ol>
<p>Disadvantages of MyISAM</p>
<ol>
<li>No data integrity (e.g. relationship constraints) check, which then comes a responsibility and overhead of the database administrators and application developers.</li>
<li>Doesn’t support transactions which is essential in critical data applications such as that of banking.</li>
<li>Slower than InnoDB for tables that are frequently being inserted to or updated, because the entire table is locked for any insert or update.</li>
</ol>
<p>The comparison is pretty straightforward. InnoDB is more suitable for data critical situations that require frequent inserts and updates. MyISAM, on the other hand, performs better with applications that don’t quite depend on the data integrity and mostly just select and display the data.</p>
<p><strong>When to use MyISAM?</strong></p>
<p>MyISAM is designed with the idea that your database is queried far more than its updated and as a result it performs very fast read operations. If your read to write(insert|update) ratio is less than 15% its better to use MyISAM.</p>
<p><strong>When to use InnoDB?</strong></p>
<p>InnoDB uses row level locking, has commit, rollback, and crash-recovery capabilities to protect user data. It supports transaction and fault tolerance.</p>
<p>Reference: <a href="http://www.kavoir.com/2009/09/mysql-engines-innodb-vs-myisam-a-comparison-of-pros-and-cons.html" rel="nofollow">Comparison InnoDB and MyISAM</a></p>
<p>You can also check it out here for further details: <a href="http://linuxers.org/howto/myisam-or-innodb-mysql-engine" rel="nofollow">MyISAM Or InnoDB MySQL engine?</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/139/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=139&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2011/12/24/innodb-vs-myisam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>google app engine to send and receive emails in rails</title>
		<link>http://shivsingh.wordpress.com/2010/04/16/google-app-engine-to-send-and-receive-emails-in-rails/</link>
		<comments>http://shivsingh.wordpress.com/2010/04/16/google-app-engine-to-send-and-receive-emails-in-rails/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 07:07:10 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[web 2.0 news]]></category>
		<category><![CDATA[gogle smpt]]></category>
		<category><![CDATA[rails email]]></category>
		<category><![CDATA[rails google]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=133</guid>
		<description><![CDATA[Sending e-mail from Rails applications is a common activity, but not all application servers are running a reliable SMTP daemon.. &#8230;<p><a href="http://shivsingh.wordpress.com/2010/04/16/google-app-engine-to-send-and-receive-emails-in-rails/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=133&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="entrytext">
<p>Sending e-mail from Rails applications is a common activity, but not all application servers are running a reliable SMTP daemon.. and setting up a server to receive e-mails is a real pain. A common solution is <a href="http://www.rubyinside.com/how-to-use-gmails-smtp-server-with-rails-394.html">to use GMail</a>, but now there&#8217;s a slicker RESTian alternative in the shape of <a href="http://leadthinking.com/233-sending-or-receiving-emails-with-rails">Remail</a> (or <a href="http://github.com/maccman/remail">GitHub repo</a>) by Alex MacCaw.</p>
<p><a href="http://leadthinking.com/233-sending-or-receiving-emails-with-rails">Remail</a> is an attempt to bring RESTian practices to the world of sending and receiving e-mail from Rails applications. In <a href="http://leadthinking.com/233-sending-or-receiving-emails-with-rails">this short &#8220;how to&#8221; guide</a>, Alex demonstrates how to get it all up and running, but effectively it relies on deploying a &#8220;Remail engine&#8221; on to a free Google App Engine project (which you can create) and then sending and receiving your mails over a REST interface through the Google hosted app. The e-mail handiwork is then managed at Google&#8217;s end.</p>
<p>Limitations? Google App Engine only lets you send 2000 e-mails a day on free accounts (this shouldn&#8217;t be a big deal to most users, though) but with the reasonably priced paid-for accounts, millions of e-mails per day can be delivered reliably. The ability to receive e-mail also makes this a must-see.</p>
<p align="left">
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/133/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/133/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=133&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/04/16/google-app-engine-to-send-and-receive-emails-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>what is dryml</title>
		<link>http://shivsingh.wordpress.com/2010/04/16/what-is-dryml/</link>
		<comments>http://shivsingh.wordpress.com/2010/04/16/what-is-dryml/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 06:59:17 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[web 2.0 news]]></category>
		<category><![CDATA[dryml]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[hobo]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=129</guid>
		<description><![CDATA[The DRYML Guide Welcome to the DRYML Guide. If you want to learn all the ins and outs of DRYML &#8230;<p><a href="http://shivsingh.wordpress.com/2010/04/16/what-is-dryml/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=129&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<div>
<p>The DRYML Guide</p>
<p>Welcome to the DRYML Guide. If you want to learn all the ins and outs of DRYML and become a master of quick and elegant view templates, you’re in the right place. If you’re very new to Hobo and DRYML you might be better off with something like the Agility tutorial. This guide is somewhere between a tutorial and a reference, designed to fill in the gaps for people who have already got the hang of the basics.</p>
<p>Contents</p>
<div>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#what_is_dryml">What is DRYML?</a></li>
<li><a href="#simple_page_templates_and_erb">Simple page templates and ERB</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#no_erb_inside_tags">No ERB inside tags</a></li>
<li><a href="#where_are_the_layouts">Where are the layouts?</a></li>
</ul>
</li>
<li><a href="#defining_simple_tags">Defining simple tags</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#parameters">Parameters</a></li>
<li><a href="#changing_parameter_names">Changing Parameter Names</a></li>
<li><a href="#multiple_parameters">Multiple Parameters</a></li>
<li><a href="#default_parameter_content">Default Parameter Content</a></li>
<li><a href="#nested__declarations">Nested <code>param</code> Declarations</a></li>
<li><a href="#the_default_parameter">The Default Parameter</a></li>
</ul>
</li>
<li><a href="#the_implicit_context">The Implicit Context</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#_and__attributes"><code>with</code> and <code>field</code> attributes</a></li>
<li><a href="#field_chains">Field chains</a></li>
<li><a href="#_and_"><code>this_field</code> and <code>this_parent</code></a></li>
<li><a href="#numeric_field_indices">Numeric field indices</a></li>
<li><a href="#forms">Forms</a></li>
</ul>
</li>
<li><a href="#tag_attributes">Tag attributes</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#flag_attributes">Flag attributes</a></li>
<li><a href="#_and__locals"><code>attributes</code> and <code>all_attributes</code> locals</a></li>
<li><a href="#merging_attributes">Merging Attributes</a></li>
<li><a href="#merging_selected_attributes">Merging selected attributes</a></li>
<li><a href="#the_class_attribute">The class attribute</a></li>
</ul>
</li>
<li><a href="#repeated_and_optional_content">Repeated and optional content</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#conditionals__if_and_unless">Conditionals &#8211; if and unless</a></li>
<li><a href="#repetition">Repetition</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#evenodd_classes">even/odd classes</a></li>
<li><a href="#_and__helpers"><code>first_item?</code> and <code>last_item?</code> helpers</a></li>
<li><a href="#repeating_over_hashes">Repeating over hashes</a></li>
</ul>
</li>
<li><a href="#using_the_implicit_context">Using the implicit context</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#one_last_shorthand__attributes_of_">One last shorthand &#8211; attributes of <code>this</code></a></li>
</ul>
</li>
<li><a href="#content_tags_only">Content tags only</a></li>
</ul>
</li>
<li><a href="#pseudo_parameters______and_">Pseudo parameters &#8211; <code>before</code>, <code>after</code>, <code>append</code>, <code>prepend</code>, and <code>replace</code></a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#inserting_extra_content">Inserting extra content</a></li>
<li><a href="#the_default_parameter_supports_append_and_prepend">The default parameter supports append and prepend</a></li>
<li><a href="#replacing_a_parameter_entirely">Replacing a parameter entirely</a></li>
<li><a href="#current_limitation">Current limitation</a></li>
</ul>
</li>
<li><a href="#nested_parameters">Nested parameters</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#an_example">An example</a></li>
</ul>
</li>
<li><a href="#customising_and_extending_tags">Customising and extending tags</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#new_tags_from_old">New tags from old</a></li>
<li><a href="#_again"><code>merge-attrs</code> again</a></li>
<li><a href="#id92"><code>merge-params</code></a></li>
<li><a href="#id93"><code>merge</code></a></li>
<li><a href="#merging_selected_parameters">Merging selected parameters</a></li>
<li><a href="#extending_a_tag">Extending a tag</a></li>
</ul>
</li>
<li><a href="#aliasing_tags">Aliasing tags</a></li>
<li><a href="#polymorphic_tags">Polymorphic tags</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#type_hierarchy">Type hierarchy</a></li>
<li><a href="#specifying_the_type_explicitly">Specifying the type explicitly</a></li>
<li><a href="#extending_polymorphic_tags">Extending polymorphic tags</a></li>
</ul>
</li>
<li><a href="#wrapping_content">Wrapping content</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#wrapping_inside_a_parameter">Wrapping <em>inside</em> a parameter</a></li>
<li><a href="#wrapping_outside_a_parameter">Wrapping <em>outside</em> a parameter</a></li>
</ul>
</li>
<li><a href="#local_variables_and_scoped_variables">Local variables and scoped variables.</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#setting_local_variables_with_">Setting local variables with <code>&lt;set&gt;</code></a></li>
<li><a href="#scoped_variables__">Scoped variables &ndash; <code>&lt;set-scoped&gt;</code></a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#nested_scopes">Nested scopes</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#taglibs">Taglibs</a></li>
<li><a href="#divergences_from_xml_and_html">Divergences from XML and HTML</a>
<ul style="list-style-type:none;list-style-image:none;list-style-position:outside;">
<li><a href="#selfclosing_tags">Self-closing tags</a></li>
<li><a href="#colons_in_tag_names">Colons in tag names</a></li>
<li><a href="#close_tag_shortcuts">Close tag shortcuts</a></li>
<li><a href="#null_end_tags">Null end tags</a></li>
</ul>
</li>
</ul>
</div>
<h1 id="what_is_dryml">What is DRYML?</h1>
<p>DRYML is a template language for Ruby on Rails that you can use in place of Rails’ built-in ERB templates. It is part of the larger Hobo project, but will eventually be made available as a separate plugin. DRYML was created in response to the observation that the vast majority of Rails development time seems to be spent in the view-layer. Rails’ models are beautifully declarative, the controllers can be made so pretty easily (witness the many and various “result controller” plugins), but the views, ah the views…</p>
<p>Given that so much of the user interaction we encounter on the web is so similar from one website to another, surely we don’t have to code all this stuff up from low-level primitives over and over again? Please, no! Of course what we want is a nice library of ready-to-go user interface components, or widgets, which can be quickly added to our project, and easily tailored to the specifics of our application.</p>
<p>If you’ve been at this game for a while you’re probably frowning about now. Re-use is a very, very thorny problem. It’s one of those things that sounds straight-forward and obvious in principle, but turns out to be horribly difficult in practice. When you come to re-use something, you very often find that your new needs differ from the original ones in a way that wasn’t foreseen or catered for in the design of the component. The more complex the component, the more likely it is that bending the thing to your needs will be harder than starting again from scratch.</p>
<p>So the challenge is not in being able to re-use code, it is in being able to re-use code in ways that were not foreseen. The reason we created DRYML was to see if this kind of flexibility could be built into the language itself. DRYML is a tag-based language that makes it trivially easy to give the defined tags a great deal of flexibility.</p>
<p>So DRYML is just a means to an end. The real goal is to create a library of reusable user-interface components that actually succeed in making it very quick and easy to create the view layer of a web application. That library is also part of Hobo &ndash; the <em>Rapid</em> tag library, but Rapid is not covered in this guide. Here we will see how DRYML provides the tools and raw materials that make a library like Rapid possible.</p>
<p>Not covering Rapid means that many of the examples are <em>not</em> good advice for use of DRYML in a full Hobo app. For example, in this guide you might see</p>
<pre><code>&lt;%= h this.name %&gt;</code></pre>
<p>Which in an app that used Rapid would be better written <code>&lt;view:name/&gt;</code> or even just <code>&lt;name/&gt;</code> (that’s a tag by the way, called <code>name</code>, not some metaprogramming trick that lets you use field names as tags). Bear that in mind while you’re reading this guide. The examples are chosen to illustrate the point at hand, they are not necessarily something you want to paste right into your application.</p>
<h1 id="simple_page_templates_and_erb">Simple page templates and ERB</h1>
<p>In its most basic usage, DRYML can be indistinguishable from a normal Rails template. That’s because DRYML is (almost) an extension of ERB, so you can still insert Ruby snippets using the <code>&lt;% ... %&gt;</code> notation. For example, a show-page for a blog post might look like this:</p>
<pre><code>&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body</span>&gt;
&lt;<span>h1</span>&gt;My Famous Blog!&lt;/<span>h1</span>&gt;
&lt;<span>h2</span>&gt;&lt;%= @post.title %&gt;&lt;/<span>h2</span>&gt;

&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;%= @post.body %&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>body</span>&gt;
&lt;/<span>html</span>&gt;</code></pre>
<h2 id="no_erb_inside_tags">No ERB inside tags</h2>
<p>DRYML’s support for ERB is not <em>quite</em> the same as true ERB templates. The one thing you can’t do is use ERB snippets inside a tag. To have the value of an attribute generated dynamically in ERB, you could do:</p>
<pre><code>&lt;<span>a </span><span>href</span>=<span>"&lt;%= my_url %&gt;"</span>&gt;</code></pre>
<p>In DRYML you would do:</p>
<pre><code>&lt;<span>a </span><span>href</span>=<span>"#{my_url}"</span>&gt;</code></pre>
<p>In rare cases, you might use an ERB snippet to output one or more entire attributes:</p>
<pre><code>&lt;<span>form </span>&lt;%= my_attributes %&gt;&gt;</code></pre>
<p>We’re jumping ahead here, so just skip this if it doesn’t make sense, but to do the equivalent in DRYML, you would need your attributes to be in a hash (rather than a string), and do:</p>
<pre><code>&lt;<span>form </span><span>merge-attrs</span>=<span>"&amp;my_attributes"</span>&gt;</code></pre>
<p>Finally, in a rare case you could even use an ERB snippet to generate the tag name itself:</p>
<pre><code>&lt;&lt;%= my_tag_name %&gt;&gt; ... &lt;/&lt;%= my_tag_name %&gt;&gt;</code></pre>
<p>To achieve that in DRYML, you could put the angle brackets in the snippet too:</p>
<pre><code>&lt;%= <span>"&lt;#{my_tag_name}&gt;"</span> %&gt; ... &lt;%= <span>"&lt;/#{my_tag_name}&gt;"</span> %&gt;</code></pre>
<h2 id="where_are_the_layouts">Where are the layouts?</h2>
<p>Going back to the <code>&lt;page&gt;</code> tag at the start of this section, from a “normal Rails” perspective, you might be wondering why the boilerplate stuff like <code>&lt;html&gt;</code>, <code>&lt;head&gt;</code> and <code>&lt;body&gt;</code> are there. What happened to layouts? You don’t tend to use layouts with DRYML, instead you would define your own tag, typically <code>&lt;page&gt;</code>, and call that. Using tags for layouts is much more flexible, and it moves the choice of layout out of the controller and into the view layer, where it should be.</p>
<p>We’ll see how to define a <code>&lt;page&gt;</code> tag in the next section.</p>
<h1 id="defining_simple_tags">Defining simple tags</h1>
<p>One of the strengths of DRYML is that defining tags is done right in the template (or in an imported tag library) using the same XML-like syntax. This means that if you’ve got markup you want to re-use, you can simply cut-and-paste it into a tag definition.</p>
<p>Here’s the page from the previous section, defined as a <code>&lt;page&gt;</code> tag simply by wrapping the markup in a <code>&lt;def&gt;</code> tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body</span>&gt;
&lt;<span>h1</span>&gt;My Famous Blog!&lt;/<span>h1</span>&gt;
&lt;<span>h2</span>&gt;&lt;%= @post.title %&gt;&lt;/<span>h2</span>&gt;

&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;%= @post.body %&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>body</span>&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now we can call that tag just as we would call any other:</p>
<pre><code>&lt;<span>page</span>/&gt;</code></pre>
<p>If you’d like an analogy to “normal” programming, you can think of the <code>&lt;def&gt;...&lt;/def&gt;</code> as defining a method called <code>page</code>, and <code>&lt;page/&gt;</code> as a call to that method. In fact, DRYML is implemented by compiling to Ruby, and that is exactly what is happening.</p>
<h2 id="parameters">Parameters</h2>
<p>We’ve illustrated the most basic usage of <code>&lt;def&gt;</code>, but our <code>&lt;page&gt;</code> tag is not very useful. Let’s take it a step further to make it into the equivalent of a layout. First of all, we clearly need the body of the page to be different each time we call it. In DRYML we achieve this by adding <em>parameters</em> to the definition, which is accomplished with the <code>param</code> attribute. Here’s the new definition:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body </span><span>param</span>/&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now we can call the <code>&lt;page&gt;</code> tag and provide our own body:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>body:</span>&gt;
&lt;<span>h1</span>&gt;My Famous Blog!&lt;/<span>h1</span>&gt;
&lt;<span>h2</span>&gt;&lt;%= @post.title %&gt;&lt;/<span>h2</span>&gt;

&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;%= @post.body %&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>body:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>See how easy that was? We just added <code>param</code> to the <code>&lt;body&gt;</code> tag, which means our page tag now has a parameter called <code>body</code>. In the <code>&lt;page&gt;</code> call we provide some content for that parameter. It’s very important to read that call to <code>&lt;page&gt;</code> properly. In particular, the <code>&lt;body:&gt;</code> (note the trailing ’:’) is <em>not</em> a call to a tag, it is providing a named parameter to the call to <code>&lt;page&gt;</code>. We call <code>&lt;body:&gt;</code> a <em>parameter tag</em>. In Ruby terms you could think of the call like this:</p>
<pre><code>page<span>(</span>:body =&gt; <span>"...my body content..."</span><span>)</span></code></pre>
<p>Note that is not actually what the compiled Ruby looks like in this case, but it illustrates the important point that <code>&lt;page&gt;</code> is a call to a defined tag, whereas <code>&lt;body:&gt;</code> is providing a parameter to that call.</p>
<h2 id="changing_parameter_names">Changing Parameter Names</h2>
<p>To give the parameter a different name, we can provide a value to the <code>param</code> attribute:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body </span><span>param</span>=<span>"content"</span>/&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We would now call the tag like this:</p>
<pre><code>&lt;<span>page</span>&gt;&lt;<span>content:</span>&gt; ...body content goes here... &lt;/<span>content:</span>&gt;&lt;/<span>page</span>&gt;</code></pre>
<h2 id="multiple_parameters">Multiple Parameters</h2>
<p>As you would expect, we can define many parameters in a single tag. For example, here’s a page with a side-bar:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"content"</span>/&gt;
&lt;<span>div </span><span>param</span>=<span>"aside"</span> /&gt;
&lt;/<span>body</span>&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Which we could call like this:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>content:</span>&gt; ... main content here ... &lt;/<span>content:</span>&gt;
&lt;<span>aside:</span>&gt;  ... aside content here ... &lt;/<span>aside:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>Note that when you name a parameter, DRYML automatically adds a CSS class of the same name to the output, so the two <code>&lt;div&gt;</code> tags above will be output as <code>&lt;div&gt;</code> and <code>&lt;div&gt;</code> respectively.</p>
<h2 id="default_parameter_content">Default Parameter Content</h2>
<p>In the examples we’ve seen so far, we’ve only put the <code>param</code> attribute on empty tags. That’s not required though. If you declare a non-empty tag as a parameter, the content of that tag becomes the default when the call does not provide that parameter. This means you can easily add a parameter to any part of the template that you think the caller might want to be able to change:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title </span><span>param</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body </span><span>param</span>&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We’ve made the page title parameterised. All existing calls to <code>&lt;page/&gt;</code> will continue to work unchanged, but we’ve now got the ability to change the title on a per-page basis:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>title:</span>&gt;My VERY EXCITING Blog&lt;/<span>title:</span>&gt;
&lt;<span>body:</span>&gt;
... body content
&lt;/<span>body:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>This is a very nice feature of DRYML &#8211; whenever you’re writing a tag, and you see a part that might be useful to change in some situations, just throw the <code>param</code> attribute at it and you’re done.</p>
<h2 id="nested__declarations">Nested <code>param</code> Declarations</h2>
<p>You can nest <code>param</code> declarations inside other tags that have <code>param</code> on them. For example, there’s no need to choose between a <code>&lt;page&gt;</code> tag that provides a single content section and one that provides an aside section as well &ndash; a single definition can serve both purposes:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body </span><span>param</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"content"</span>/&gt;
&lt;<span>div </span><span>param</span>=<span>"aside"</span> /&gt;
&lt;/<span>body</span>&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Here the <code>&lt;body&gt;</code> tag is a <code>param</code>, and so are the two <code>&lt;div&gt;</code> tags inside it. The <code>&lt;page&gt;</code> tag can be called either like this:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>body:</span>&gt; ... page content goes here ... &lt;/<span>body:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>Or like this:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>content:</span>&gt; ... main content here ... &lt;/<span>content:</span>&gt;
&lt;<span>aside:</span>&gt;  ... aside content here ... &lt;/<span>aside:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>An interesting question is, what happens if you give both a <code>&lt;body:&gt;</code> parameter and say, <code>&lt;content:&gt;</code>. By providing the <code>&lt;body:&gt;</code> parameter, you have replaced everything inside the body section, including those two parameterised <code>&lt;div&gt;</code> tags, so the <code>&lt;body:&gt;</code> you have provided will appear as normal, but the <code>&lt;content:&gt;</code> parameter will be silently ignored.</p>
<h2 id="the_default_parameter">The Default Parameter</h2>
<p>In the situation where a tag will usually be given a single parameter when called, you can give your tag a more compact XML-like syntax by using the special parameter name <code>default</code>:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt;
&lt;<span>title</span>&gt;My Blog&lt;/<span>title</span>&gt;
&lt;/<span>head</span>&gt;
&lt;<span>body </span><span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now there is no need to give a parameter tag in the call at all &#8211; the content directly inside the <code>&lt;page&gt;</code> tag becomes the <code>default</code> parameter:</p>
<pre><code>&lt;page&gt; ... body content goes here -- no need for a parameter tag ... &lt;/page&gt;</code></pre>
<p>You might notice that the <code>&lt;page&gt;</code> tag is now indistinguishable from a normal HTML tag. Some find this aspect of DRYML disconcerting at first &ndash; how can you tell what is an HTML tag and what it a defined DRYML tag? The answer is &ndash; you can’t, and that’s quite deliberate. This allows you to do nice tricks like define your own smart <code>&lt;form&gt;</code> tag or <code>&lt;a&gt;</code> tag (the Rapid library does exactly that). Other tag-based template languages (e.g. Java’s JSP) like to put everything in XML namespaces. The result is very cluttered views that are boring to type and hard to read. From the start we put a very high priority on making DRYML templates compact and elegant. When you’re new to DRYML you might have to do a lot of looking things up, as you would with any new language or API, but things gradually become familiar and then view templates can be read and understood very easily.</p>
<h1 id="the_implicit_context">The Implicit Context</h1>
<p>In addition to the most important goal behind DRYML &#8211; creating a template language that would encourage re-use in the view layer, a secondary goal is for templates to be concise, elegant and readable. One aspect of DRYML that helps a lot in this regard is something called the <em>implicit context</em>.</p>
<p>This feature was born of a simple observation that pretty much every page in a web app renders some kind of hierarchy of application objects. Think about a simple page in a blog &#8211; say, the permalink page for an individual post. The page as a whole can be considered a rendering of a BlogPost object. Then we have sections of the page that display different “pieces” of the post &ndash; the title, the date, the author’s name, the body. Then we have the comments. The list of comments as a whole is also a “piece” of the BlogPost. Within that we have each of the individual comments, and the whole thing starts again: the comment title, date, author… This can carry on even further, for example some blogs are set up so that you can comment on comments.</p>
<p>This structure is incredibly common, perhaps even universal, as it seems to be intrinsically tied to the way we visually parse information. DRYML’s implicit context takes advantage of this fact to make templates extremely concise while remaining readable and clear. The object that you are rendering in any part of the page is known as the <em>context</em>, and every tag has access to this object through the method <code>this</code>. The controller sets up the initial context, and the templates then only have to mention where the context needs to <em>change</em>.</p>
<p>We’ll dive straight into some examples, but first a quick general point about this guide. If you like to use the full Hobo framework, you will probably always use DRYML and the Rapid tag library together. DRYML and Rapid have grown up together, and the design of each is heavily influenced by the other. Having said that, this is the DRYML Guide, not the Rapid Guide. We won’t be using any Rapid tags in this guide, because we want to document DRYML the language properly. That will possibly be a source of confusion if you’re very used to working with Rapid. Just keep in mind that we’re not allowed to use any Rapid tags in this guide and you’ll be fine.</p>
<p>In order to see the implicit context in its best light, we’ll start by defining a <code>&lt;view&gt;</code> tag, that simply renders the current context with HTML escaping. Remember the context is always available as <code>this</code>:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"view"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>def</span>&gt;</code></pre>
<p>Next we’ll define a tag for making a link to the current context. We’ll assume the object will be recognised by Rails’ polymorphic routing. Let’s call the tag <code>&lt;l&gt;</code> (for link):</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"l"</span>&gt;&lt;<span>a </span><span>href</span>=<span>"#{url_for this}"</span> <span>param</span>=<span>"default"</span>/&gt;&lt;/<span>def</span>&gt;</code></pre>
<p>Now let’s use these tags in a page template. We’ll stick with the comfortingly boring blog post example. In order to set the initial context, our controller action would need to do something like this:</p>
<pre><code><span>def</span> show
@this = @blog_post = BlogPost.find<span>(</span>params<span>[:id</span>]<span>)</span>
<span>end</span></code></pre>
<p>The DRYML template handler looks for the <code>@this</code> instance variable for the initial context. It’s quite nice to also set the more conventionally named instance variable as we’ve done here.</p>
<p>Now we’ll create the page. Let’s assume we’re using a <code>&lt;page&gt;</code> tag along the lines of those defined above. We’ll also assume that the blog post object has these fields: <code>title</code>, <code>published_at</code>, <code>body</code> and <code>belongs_to :author</code>, and that the author has a <code>name</code> field:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>content:</span>&gt;
&lt;<span>h2</span>&gt;&lt;<span>view</span><span>:title</span>/&gt;&lt;/<span>h2</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"details"</span>&gt;
Published by &lt;<span>l</span><span>:author</span>&gt;&lt;<span>view</span><span>:name</span>/&gt;&lt;/<span>l</span>&gt; on &lt;<span>view</span><span>:published-at</span>/&gt;.
&lt;/<span>div</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;<span>view</span><span>:body</span>/&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>content:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>When you see a tag like <code>&lt;view:title/&gt;</code>, you don’t get any prizes for guessing what will be displayed. In terms of what actually happens, you can read this as “change the context to be the <code>title</code> attribute of the current context, then call the <code>&lt;view</code>&gt; tag”. You might like to think of that change to the context as <code>this = this.title</code> (although in fact <code>this</code> is not assignable). But really you just think of it as “view the title”. Of what? Of whatever is in context, in this case the blog post.</p>
<p>Be careful with the two different uses of colon in DRYML. A trailing colon as in <code>&lt;foo:&gt;</code> indicates a parameter tag, whereas a colon joining two names as in <code>&lt;view:title/&gt;</code> indicates a change of context.</p>
<p>When the tag ends, the context is set back to what it was. In the case of <code>&lt;view/&gt;</code> which is a self-closing tag familiar from XML, that happens immediately. The <code>&lt;l:author&gt;</code> tag is more interesting. We set the context to be the author, so that the link goes to the right place. Inside the <code>&lt;l:author&gt;</code> that context remains in place so we just need <code>&lt;view:name/&gt;</code> in order to display the author’s name.</p>
<h2 id="_and__attributes"><code>with</code> and <code>field</code> attributes</h2>
<p>The <code>with</code> attribute is a special DRYML attribute that sets the context to be the result of any Ruby expression before the tag is called. In DRYML any attribute value that starts with ’&amp;’ is interpreted as a Ruby expression. Here’s the same example as above using only the <code>with</code> attribute:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>content:</span>&gt;
&lt;<span>h2</span>&gt;&lt;<span>view </span><span>with</span>=<span>"&amp;@blog_post.title"</span>/&gt;&lt;/<span>h2</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"details"</span>&gt;
Published by &lt;<span>l </span><span>with</span>=<span>"&amp;@blog_post.author"</span>&gt;&lt;<span>view </span><span>with</span>=<span>"&amp;this.name"</span>/&gt;&lt;/<span>l</span>&gt;
on &lt;<span>view </span><span>with</span>=<span>"&amp;@blog_post.published-at"</span>/&gt;.
&lt;/<span>div</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;<span>view </span><span>with</span>=<span>"&amp;@blog_post.body"</span>/&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>content:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>Note that we could have used <code>&amp;this.title</code> instead of <code>&amp;@blog_post.title</code>.</p>
<p>The <code>field</code> attribute makes things more concise by taking advantage of a common pattern. When changing the context, we very often want to change to some attribute of the current context. <code>field="x"</code> is a shorthand for <code>with="&amp;this.x"</code> (actually it’s not quite the same, using the <code>field</code> version also sets <code>this_parent</code> and <code>this_field</code>, whereas <code>with</code> does not. This is discussed later in more detail).</p>
<p>The same template again, this time using <code>field</code>:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>content:</span>&gt;
&lt;<span>h2</span>&gt;&lt;<span>view </span><span>field</span>=<span>"title"</span>/&gt;&lt;/<span>h2</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"details"</span>&gt;
Published by &lt;<span>l </span><span>field</span>=<span>"author"</span>&gt;&lt;<span>view </span><span>field</span>=<span>"name"</span>/&gt;&lt;/<span>l</span>&gt;
on &lt;<span>view </span><span>field</span>=<span>"published-at"</span>/&gt;.
&lt;/<span>div</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"post-body"</span>&gt;
&lt;<span>view </span><span>field</span>=<span>"body"</span>/&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>content:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>If you compare that example to the first one, you should notice that the <code>:</code> syntax is just a shorthand for the <code>field</code> attribute; i.e., <code>&lt;view field="name"&gt;</code> and <code>&lt;view:name&gt;</code> are equivalent.</p>
<h2 id="field_chains">Field chains</h2>
<p>Sometimes you want to drill down through several fields at a time. Both the <code>field</code> attribute and the <code>:</code> shorthand support this. For example:</p>
<pre><code>&lt;<span>view</span>:category.<span>name</span>/&gt;
&lt;<span>view </span><span>field</span>=<span>"category.name"</span>/&gt;</code></pre>
<h2 id="_and_"><code>this_field</code> and <code>this_parent</code></h2>
<p>When you change the context using <code>field="my-field"</code> (or the <code>&lt;tag:my-field&gt;</code> shorthand), the previous context is available as <code>this_parent</code>, and the name of the field is available as <code>this_field</code>. If you set the context using <code>with="..."</code>, these values are not available. That means the following apparently identical tag calls are not quite the same:</p>
<pre><code>&lt;my-tag with="&amp;@post.title"/&gt;

&lt;my-tag with="&amp;@post" field="title"/&gt;</code></pre>
<p>If the tag requires <code>this_parent</code> and <code>this_field</code>, and in Rapid, for example, some do, then it must be called using the second style.</p>
<h2 id="numeric_field_indices">Numeric field indices</h2>
<p>If your current context is a collection, you can use the field attribute to change the context to a single item.</p>
<pre><code>&lt;my-tag field="7" /&gt;

&lt;% i=97 %&gt;
&lt;my-tag field="&amp;i" /&gt;</code></pre>
<p>The <code>&lt;repeat&gt;</code> tag sets <code>this_field</code> to the current index into the collection.</p>
<pre><code>&lt;repeat:foos&gt;
&lt;td&gt;&lt;%= this_field %&gt;&lt;/td&gt;
&lt;td&gt;&lt;view /&gt;&lt;/td&gt;
&lt;/repeat&gt;</code></pre>
<h2 id="forms">Forms</h2>
<p>When rendering the Rapid library’s <code>&lt;form&gt;</code> tag, DRYML keeps track of even more metadata in order to add <code>name</code> attributes to form fields automatically. This mechanism does not work if you set the context using <code>with=</code>.</p>
<h1 id="tag_attributes">Tag attributes</h1>
<p>As we’ve seen, DRYML provides parameters as a mechanism for customising the markup that is output by a tag. Sometimes we want to provide other kinds of values to control the behaviour of a tag: URLs, filenames or even Ruby values like hashes and arrays. For this situation, DRYML lets you define tag attributes.</p>
<p>As a simple example, say your application has a bunch of help files in <code>public/help</code>, and you have links to them scattered around your views. Here’s a tag you could define:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"help-link"</span> <span>attrs</span>=<span>"file"</span>&gt;
&lt;<span>a </span><span>class</span>=<span>"help"</span> <span>href</span>=<span>"#{base_url}/help/#{file}.html"</span> <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p><code>&lt;def&gt;</code> takes a special attribute <code>attrs</code>. Use this to declare a list (separated by commas) of attributes, much as you would declare arguments to a method in Ruby. Here we’ve defined one attribute, <code>file</code>, and just like arguments in Ruby, <code>file</code> becomes a local variable inside the tag definition. In this definition we construct the <code>href</code> attribute from the <code>base_url</code> helper and <code>file</code>, using Ruby string interpolation syntax (<code>#{....}</code>). Remember that you can use that syntax when providing a value for any attribute in DRYML.</p>
<p>The call to this tag would look like this:</p>
<pre><code>&lt;<span>help-link </span><span>file</span>=<span>"intro"</span>&gt;Introductory Help&lt;/<span>help-link</span>&gt;</code></pre>
<p>Using regular XML-like attribute syntax &ndash; <code>file="intro"</code> &ndash; passes “intro” as a string value to the attribute. DRYML also allows you to pass any Ruby value. When the attribute value starts with <code>&amp;</code>, the rest of the attribute is interpreted as a Ruby expression. For example you could use this syntax to pass <code>true</code> and <code>false</code> values:</p>
<pre><code>&lt;<span>help-link </span><span>file</span>=<span>"intro"</span> <span>new-window</span>=<span>"&amp;true"</span>&gt;Introductory Help&lt;/<span>help-link</span>&gt;
&lt;<span>help-link </span><span>file</span>=<span>"intro"</span> <span>new-window</span>=<span>"&amp;false"</span>&gt;Introductory Help&lt;/<span>help-link</span>&gt;</code></pre>
<p>And we could add that <code>new-window</code> attribute to the definition like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"help-link"</span> <span>attrs</span>=<span>"file, new-window"</span>&gt;
&lt;<span>a </span><span>class</span>=<span>"help"</span> <span>href</span>=<span>"#{base_url}/help/#{file}.html"</span>
<span>target</span>=<span>"#{new_window ? '_blank' : '_self' }"</span> <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>An important point to notice there is that the markup-friendly dash in the <code>new-window</code> attribute became a Ruby-friendly underscore (<code>new_window</code>) in the local variable inside the tag definition.</p>
<p>Using the <code>&amp;</code>, you can pass any value you like &ndash; arrays, hashes, active-record objects…</p>
<p>In the case of boolean values like the one used in the above example, there is a nicer syntax that can be used in the call…</p>
<h2 id="flag_attributes">Flag attributes</h2>
<p>That <code>new-window</code> attribute shown in the previous section is simple switch &#8211; on or off. DRYML lets you omit the value of the attribute, giving a flag-like syntax:</p>
<pre><code>&lt;<span>help-link </span><span>file</span>=<span>"intro"</span> <span>new-window</span>&gt;Introductory Help&lt;/<span>help-link</span>&gt;
&lt;<span>help-link </span><span>file</span>=<span>"intro"</span>&gt;Introductory Help&lt;/<span>help-link</span>&gt;</code></pre>
<p>Omitting the attribute value is equivalent to giving <code>"&amp;true"</code> as the value. In the second example the attribute is omitted entirely, meaning the value will be <code>nil</code> which evaluates to false in Ruby and so works as expected.</p>
<h2 id="_and__locals"><code>attributes</code> and <code>all_attributes</code> locals</h2>
<p>Inside a tag definition two hashes are available in local variables:</p>
<ul>
<li><code>attributes</code> contains all the attributes that <em>were not declared</em> in the <code>attrs</code> list of the <code>def</code> but that were provided in the call to the tag.</li>
<li><code>all_attributes</code> contains every attribute, including the declared ones.</li>
</ul>
<h2 id="merging_attributes">Merging Attributes</h2>
<p>In a tag definition, you can use the <code>merge-attrs</code> attribute to take any ‘extra’ attributes that the caller passed in, and add them to a tag of your choosing inside your definition. Let’s backtrack a bit and see why you might want to do that.</p>
<p>Here’s a simple definition for a <code>&lt;markdown-help&gt;</code> tag- it’s similar to a tag defined in the Hobo Cookbook app:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"markdown-help"</span>&gt;
&lt;<span>a </span><span>href</span>=<span>"http://daringfireball.net/..."</span> <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>You would use it like this:</p>
<pre><code>Add formatting using &lt;<span>markdown-help</span>&gt;markdown&lt;/<span>markdown-help</span>&gt;</code></pre>
<p>Suppose you wanted to give the caller the ability to choose the <code>target</code> for the link. You could extend the definition like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"markdown-help"</span> <span>attrs</span>=<span>"target"</span>&gt;
&lt;<span>a </span><span>href</span>=<span>"http://daringfireball.net/..."</span> <span>target</span>=<span>"&amp;target"</span> <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now we can call the tag like this:</p>
<pre><code>Add formatting using &lt;<span>markdown-help </span><span>target</span>=<span>"_blank"</span>&gt;markdown&lt;/<span>markdown-help</span>&gt;</code></pre>
<p>OK, but maybe the caller wants to add a CSS class, or a javascript <code>onclick</code> attribute, or any one of a dozen potential HTML attributes. This approach is not going to scale. That’s where <code>merge-attrs</code> comes in. As mentioned above, DRYML keeps track of all the attributes that were passed to a tag, even if they were not declared in the <code>attrs</code> list of the tag definition. They are available in two hashes: <code>attributes</code> (that has only undeclared attributes) and <code>all_attributes</code> (that has all of them), but in normal usage you don’t need to access those variables directly. To add all of the undeclared attributes to a tag inside your definition, just add the <code>merge-attrs</code> attribute, like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"markdown-help"</span>&gt;
&lt;<span>a </span><span>href</span>=<span>"http://daringfireball.net/..."</span> merge-attrs <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Note that the <code>merge</code> attribute is another way of merging attributes. Declaring <code>merge</code> is a shorthand for declaring both <code>merge-attrs</code> and <code>merge-params</code> (which we’ll cover later).</p>
<h2 id="merging_selected_attributes">Merging selected attributes</h2>
<p><code>merge-attrs</code> can be given a value &#8211; either a hash containing attribute names and values, or a list of attribute names (comma separated), to be merged from the <code>all_attributes</code> variable.</p>
<p>Examples:</p>
<pre><code>&lt;<span>a </span><span>merge-attrs</span>=<span>"href, name"</span>&gt;

&lt;<span>a </span><span>merge-attrs</span>=<span>"&amp;my_attribute_hash"</span>&gt;</code></pre>
<p>A requirement that crops up from time to time is to forward to a tag all the attributes that it understands (i.e. the attributes from that tag’s <code>attrs</code> list), and to forward some or all the other attributes to tags called within that tag. Say for example, we are declaring a tag that renders a section of content, with some navigation at the top. We want to be able to add CSS classes and so on to the main <code>&lt;div&gt;</code> that will be output, but the <code>&lt;navigation&gt;</code> tag also defines some special attributes, and these need to be forwarded to it.</p>
<p>To achieve this we take advantage of a helper method <code>attrs_for</code>. Given the name of a tag, it returns the list of attributes declared by that tag.</p>
<p>Here’s the definition:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"section-with-nav"</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"section"</span> <span>merge-attrs</span>=<span>"&amp;attributes - attrs_for(:navigation)"</span>&gt;
&lt;<span>navigation </span><span>merge-attrs</span>=<span>"&amp;attributes &amp; attrs_for(:navigation)"</span>/&gt;
&lt;<span>do </span><span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Note that:</p>
<ul>
<li>
<p>The expression <code>attributes - attrs_for(:navigation)</code> returns a hash of only those attributes from the <code>attributes</code> hash that are <em>not</em> declared by <code>&lt;navigation&gt;</code> (The <code>-</code> operator on <code>Hash</code> comes from HoboSupport)</p>
</li>
<li>
<p>The expression <code>attributes &amp; attrs_for(:navigation)</code> returns a hash of only those attributes from the <code>attributes</code> hash that <em>are</em> declared by <code>&lt;navigation&gt;</code> (The <code>&amp;</code> operator on <code>Hash</code> comes from HoboSupport)</p>
</li>
<li>
<p>The <code>&lt;do&gt;</code> tag is a “do nothing” tag, defined by the core DRYML taglib, which is always included.</p>
</li>
</ul>
<h2 id="the_class_attribute">The class attribute</h2>
<p>If you have the following definition:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"foo"</span>&gt;
&lt;<span>div </span><span>id</span>=<span>"foo"</span> <span>class</span>=<span>"bar"</span> merge-attrs /&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>and the user invokes it with:</p>
<pre><code>&lt;<span>foo </span><span>id</span>=<span>"baz"</span> <span>class</span>=<span>"bop"</span> /&gt;</code></pre>
<p>The following content will result:</p>
<pre><code>&lt;foo id="baz" /&gt;</code></pre>
<p>The <code>class</code> attribute receives special behaviour when merging. All other attributes are overridden with the user specified values. The <code>class</code> attribute takes on the values from both the tag definition and invocation.</p>
<h1 id="repeated_and_optional_content">Repeated and optional content</h1>
<p>As you would expect from any template language, DRYML has the facility to repeat sections of content, and to optionally render or not render given sections according to your application’s data. DRYML provides two alternative syntaxes, much as Ruby does (e.g. Ruby has the block <code>if</code> and the one-line suffix version of <code>if</code>).</p>
<h2 id="conditionals__if_and_unless">Conditionals &#8211; if and unless</h2>
<p>DRYML provides <code>if</code> and <code>unless</code> both as tags, which come from the core tag library, and are just ordinary tag definitions, and as attributes, which are part of the language:</p>
<p>The tag version:</p>
<pre><code>&lt;<span>if </span><span>test</span>=<span>"&amp;logged_in?"</span>&gt;&lt;<span>p</span>&gt;Welcome back&lt;/<span>p</span>&gt;&lt;/<span>if</span>&gt;</code></pre>
<p>The attribute version:</p>
<pre><code>&lt;<span>p </span><span>if</span>=<span>"&amp;logged_in?"</span>&gt;Welcome back&lt;/<span>p</span>&gt;</code></pre>
<p>Important note! The test is performed (in Ruby terms) like this:</p>
<pre><code><span>if</span> <span>(</span>...your test expression...<span>)</span>.blank?</code></pre>
<p>Got that? Blankiness not truthiness (<code>blank?</code> comes from ActiveSupport by the way &ndash; Rails’ mixed bag of core-Ruby extensions). So for example, in DRYML:</p>
<pre><code>&lt;<span>if </span><span>test</span>=<span>"&amp;current_user.comments"</span>&gt;</code></pre>
<p>is a test to see if there are any comments &ndash; empty collections are considered blank. We are of the opinion that Matz made a fantastic choice for Ruby when he followed the Lisp / Smalltalk approach to truth values, but that view templates are a special case, and testing for blankness is more often what you want.</p>
<p>Can we skip <code>&lt;unless&gt;</code>? It’s like <code>&lt;if&gt;</code> with the nest negated. You get the picture, right?</p>
<h2 id="repetition">Repetition</h2>
<p>For repeating sections of content, DRYML has the <code>&lt;repeat&gt;</code> tag (from the core tag library) and the <code>repeat</code> attribute.</p>
<p>The tag version:</p>
<pre><code>&lt;<span>repeat </span><span>with</span>=<span>"&amp;current_user.new_messages"</span>&gt; &lt;<span>h3</span>&gt;&lt;%= h this.subject %&gt;&lt;/<span>h3</span>&gt; &lt;/<span>repeat</span>&gt;</code></pre>
<p>The attribute version:</p>
<pre><code>&lt;<span>h3 </span><span>repeat</span>=<span>"&amp;current_user.new_messages"</span>&gt;&lt;%= h this.subject %&gt;&lt;/<span>h3</span>&gt;</code></pre>
<p>Notice that as well as the content being repeated, the implicit context is set to each item in the collection in turn.</p>
<h3 id="evenodd_classes">even/odd classes</h3>
<p>It’s a common need to want alternating styles for items in a collection &#8211; e.g. striped table rows. Both the repeat attribute and the repeat tag set a scoped variable <code>scope.even_odd</code> which will be alternately ‘even’ then ‘odd’, so you could do:</p>
<pre><code>&lt;<span>h3 </span><span>repeat</span>=<span>"&amp;new_messages"</span> <span>class</span>=<span>"#{scope.even_odd}"</span>&gt;&lt;%= h this.subject %&gt;&lt;/<span>h3</span>&gt;</code></pre>
<p>That example illustrates another important point &ndash; any Ruby code in attributes is evaluated <em>inside</em> the repeat. In other words, the <code>repeat</code> attribute behaves the same as wrapping the tag in a <code>&lt;repeat&gt;</code> tag.</p>
<h3 id="_and__helpers"><code>first_item?</code> and <code>last_item?</code> helpers</h3>
<p>Another common need is to give special treatment to the first and last items in a collection. The <code>first_item?</code> and <code>last_item?</code> helpers can be used to find out when these items come up; e.g., we could use <code>first_item?</code> to capitalise the first item:</p>
<pre><code>&lt;<span>h3 </span><span>repeat</span>=<span>"&amp;new_messages"</span>&gt;&lt;%= h(first_item? ? this.subject.upcase : this.subject) %&gt;&lt;/<span>h3</span>&gt;</code></pre>
<h3 id="repeating_over_hashes">Repeating over hashes</h3>
<p>If you give a hash as the value to repeat over, DRYML will iterate over each key/value pair, with the value available as <code>this</code> (i.e. the implicit context) and the key available as <code>this_key</code>. This is particularly useful for grouping things in combination with the <code>group_by</code> method:</p>
<pre><code>&lt;<span>div </span><span>repeat</span>=<span>"&amp;current_user.new_messages.group_by(&amp;:sender)"</span>&gt;
&lt;<span>h2</span>&gt;Messages from &lt;%= h this_key %&gt;&lt;/<span>h2</span>&gt;
&lt;<span>ul</span>&gt;
&lt;<span>li </span><span>repeat</span>&gt;&lt;%= h this.subject %&gt;&lt;/<span>li</span>&gt;
&lt;/<span>ul</span>&gt;
&lt;<span>h2</span>&gt;
&lt;/<span>div</span>&gt;</code></pre>
<p>That example has given a sneak preview of another point &#8211; using if/unless/repeat with the implicit context. We’ll get to that in a minute.</p>
<h2 id="using_the_implicit_context">Using the implicit context</h2>
<p>If you don’t specify the test of a conditional, or the collection to repeat over, the implicit context is used. This allows for a few nice shorthands. For example, this is a common pattern for rendering collections:</p>
<pre><code>&lt;<span>if</span><span>:comments</span>&gt;
&lt;<span>h3</span>&gt;Comments&lt;/<span>h3</span>&gt;
&lt;<span>ul</span>&gt;
&lt;<span>li </span><span>repeat</span>&gt; ... &lt;/<span>li</span>&gt;
&lt;/<span>ul</span>&gt;
&lt;/<span>if</span>&gt;</code></pre>
<p>We’re switching the context on the <code>&lt;if&gt;</code> tag to be <code>this.comments</code>, which has two effects. Firstly the comments collection is used as the test for the <code>if</code>, so the whole section including the heading will be omitted if the collection is empty (remember that <code>if</code> tests for blankness, and empty collections are considered blank). Secondly, the context is switched to be the comments collection, so that when we come to repeat the <code>&lt;li&gt;</code> tag, all we need to say is <code>repeat</code>.</p>
<h3 id="one_last_shorthand__attributes_of_">One last shorthand &#8211; attributes of <code>this</code></h3>
<p>The attribute versions of <code>if</code>/<code>unless</code> and <code>repeat</code> support a useful shortcut for accessing attributes or methods of the implicit context. If you give a literal string attribute&ndash;that is, an attribute that does not start with <code>&amp;</code>&ndash;this is interpreted as the name of a method on <code>this</code>. For example:</p>
<pre><code>&lt;<span>li </span><span>repeat</span>=<span>"comments"</span>/&gt;</code></pre>
<p>is equivalent to</p>
<pre><code>&lt;<span>li </span><span>repeat</span>=<span>"&amp;this.comments"</span>/&gt;</code></pre>
<p>Similarly</p>
<pre><code>&lt;<span>p </span><span>if</span>=<span>"sticky?"</span>&gt;This post has been marked <span>'sticky'</span>&lt;/<span>p</span>&gt;</code></pre>
<p>is equivalent to</p>
<pre><code>&lt;<span>p </span><span>if</span>=<span>"this.sticky?"</span>&gt;This post has been marked <span>'sticky'</span>&lt;/<span>p</span>&gt;</code></pre>
<p>It is a bit inconsistent that these shortcuts do not work with the tag versions of <code>&lt;if&gt;</code>, <code>&lt;unless&gt;</code> and <code>&lt;repeat&gt;</code>. This may be remedied in a future version of DRYML.</p>
<h2 id="content_tags_only">Content tags only</h2>
<p>The attributes introduced in this section &ndash; <code>repeat</code>, <code>if</code> and <code>unless</code>, can only be used on content tags, i.e. static HTML tags and defined tags. They cannot be used on tags like <code>&lt;def&gt;</code>, <code>&lt;extend&gt;</code> and <code>&lt;include&gt;</code>.</p>
<h1 id="pseudo_parameters______and_">Pseudo parameters &#8211; <code>before</code>, <code>after</code>, <code>append</code>, <code>prepend</code>, and <code>replace</code></h1>
<p>For every parameter you define in a tag, there are five “pseudo parameters” created as well. Four allow you to insert extra content without replacing existing content, and one lets you replace or remove a parameter entirely.</p>
<p>To help illustrate these, here’s a very simple <code>&lt;page&gt;</code> tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>body</span>&gt;
&lt;<span>h1 </span><span>param</span>=<span>"heading"</span>&gt;&lt;%= h @this.to_s %&gt;&lt;/<span>h1</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"content"</span>&gt;&lt;/<span>div</span>&gt;
&lt;/<span>body</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We’ve assumed that <code>@this.to_s</code> will give us the name of the object that this page is presenting.</p>
<h2 id="inserting_extra_content">Inserting extra content</h2>
<p>The output of the heading would look something like:</p>
<pre><code>&lt;h1&gt;Welcome to my new blog&lt;/h1&gt;</code></pre>
<p>Pseudo parameters give us the ability to insert extra context in four places, marked here as <code>(A)</code>, <code>(B)</code>, <code>(C)</code> and <code>(D)</code>:</p>
<pre><code>(A)&lt;h1&gt;(B)Welcome to my new blog(C)&lt;/h1&gt;(D)</code></pre>
<p>The parameters are:</p>
<ul>
<li>(A) &ndash; <code>&lt;before-heading:&gt;</code></li>
<li>(B) &ndash; <code>&lt;prepend-heading:&gt;</code></li>
<li>(C) &ndash; <code>&lt;append-heading:&gt;</code></li>
<li>(D) &ndash; <code>&lt;after-heading:&gt;</code></li>
</ul>
<p>So, for example, suppose we want to add the name of the blog to the heading:</p>
<pre><code>&lt;<span>h1 </span><span>class</span>=<span>"heading"</span>&gt;Welcome to my new blog -- The Hobo Blog&lt;/<span>h1</span>&gt;</code></pre>
<p>To achieve that on one page, we could call the <code>&lt;page&gt;</code> tag like this:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>append-heading:</span>&gt; -- The Hobo Blog&lt;/<span>append-heading:</span>&gt;
&lt;<span>body:</span>&gt;
...
&lt;/<span>body</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>Or we could go a step further and create a new page tag that added that suffix automatically. We could then use that new page tag for an entire section of our site:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"blog-page"</span>&gt;
&lt;<span>page</span>&gt;
&lt;<span>append-heading:</span>&gt; -- The Hobo Blog&lt;/<span>append-heading:</span>&gt;
&lt;<span>body:</span> <span>param</span>&gt;&lt;/<span>body</span>&gt;
&lt;/<span>page</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>(Note: we have explicitly made sure that the <code>&lt;body:&gt;</code> parameter is still available. There is a better way of achieving this using <code>merge-params</code> or <code>merge</code>, which are covered later.)</p>
<h2 id="the_default_parameter_supports_append_and_prepend">The default parameter supports append and prepend</h2>
<p>As we’ve seen, the <code>&lt;append-...:&gt;</code> and <code>&lt;prepend-...:&gt;</code> parameters insert content at the beginning and end of a tag’s content. But in the case of a defined tag that may output all sorts of other tags and may itself define many parameters, what exactly <em>is</em> the tag’s “content”? It is whatever is contained in the <code>default</code> parameter tag. So <code>&lt;append-...:&gt;</code> and <code>&lt;prepend-...:&gt;</code> only work on tags that define a default parameter.</p>
<p>For this reason, you will often see tag definitions include a <code>default</code> parameter, even though it would be rare to use it directly. It is there so that <code>&lt;append-...:&gt;</code> and <code>&lt;prepend-...:&gt;</code> work as expected.</p>
<h2 id="replacing_a_parameter_entirely">Replacing a parameter entirely</h2>
<p>So far, we’ve seen how the parameter mechanism allows us to change the attributes and content of a tag, but what if we want to remove the tag entirely? We might want a page that has no <code>&lt;h1&gt;</code> tag at all, or has <code>&lt;h2&gt;</code> instead. For that situation we can use “replace parameters”. Here’s a page with an <code>&lt;h2&gt;</code> instead of an <code>&lt;h1&gt;</code>:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>heading:</span> <span>replace</span>&gt;&lt;<span>h2</span>&gt;My Awesome Page&lt;/<span>h2</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>And here’s one with no heading at all:</p>
<pre><code>&lt;<span>page</span>&gt;
&lt;<span>heading:</span> <span>replace</span>/&gt;
&lt;/<span>page</span>&gt;</code></pre>
<p>There is a nice shorthand for the second case. For every parameter, the enclosing tag also supports a special <code>without</code> attribute. This is exactly equivalent to the previous example, but much more readable:</p>
<pre><code>&lt;<span>page </span><span>without-heading</span>/&gt;</code></pre>
<p>Note: to make things more consistent, <code>&lt;heading: replace&gt;</code> may become <code>&lt;replace-heading:&gt;</code> in the future.</p>
<h2 id="current_limitation">Current limitation</h2>
<p>Due to a limitation of the current DRYML implementation, you cannot use both <code>before</code> and <code>after</code> on the same parameter. You can achieve the same effect as follows (this technique is covered properly later in the section on wrapping content):</p>
<pre><code>&lt;<span>heading:</span> <span>replace</span>&gt;
... before content ...
&lt;<span>heading </span><span>restore</span>&gt;
... after content ...
&lt;/<span>heading:</span>&gt;</code></pre>
<h1 id="nested_parameters">Nested parameters</h1>
<p>As we’ve discussed at the start of this guide, one of the main motivations for the creation of DRYML was to deliver a higher degree of <em>re-use</em> in the view layer. One of the great challenges of re-use is managing the constant tension between re-use and flexibility: the greater the need for flexibility, the harder it is to re-use existing code. This has a very direct effect on the <em>size</em> of things that we can successfully re-use. Take the humble hypertext link for example. A link is a link is a link &ndash; there’s only so much you could really want to change, so it’s not surprising that long ago we stopped having to assemble links from fragments of HTML text. Rails has its <code>link_to</code> helper, and Hobo Rapid has its <code>&lt;a&gt;</code> tag. At the other extreme, reusing an entire photo gallery or interactive calendar is extremely difficult. Again no surprise&ndash;these things have been built from scratch over and over again, because each time something slightly (or very) different is needed. A single calendar component that is flexible enough to cover every eventuality would be so complicated that configuring it would be more effort than starting over.</p>
<p>This tension between re-use and flexibility will probably never go away; life is just like that. As components get larger they will inevitably get either harder to work with or less flexible. What we can do though, through technologies like DRYML, is slow down the onset of these problems. By thinking about the fundamental challenges to re-use, we have tried to create a language in which, as components grow larger, simplicity and flexibility can be retained longer.</p>
<p>One of the most important features that DRYML brings to the re-use party is <em>nested parameters</em>. They are born of the following observations:</p>
<ul>
<li>
<p>As components get larger, they are not really single components at all, but compositions of many smaller sub-components.</p>
</li>
<li>
<p>Often, the customisation we wish to make is not to the “super-component” but to one of the sub-components.</p>
</li>
<li>
<p>What is needed, then, is a means to pass parameters and attributes not just to the tag you are calling, but to the tag called within the tag, or the tag called within the tag called within the tag, and so on.</p>
</li>
</ul>
<p>DRYML’s nested parameter mechanism does exactly that. After you’ve been using DRYML for some time, you may notice that you don’t use this feature very often. But when you do use it, it can make the difference between sticking with your nice high-level components or throwing them away and rebuilding from scratch. A little use of nested parameters goes a long way.</p>
<h2 id="an_example">An example</h2>
<p>To illustrate the mechanism, we’ll build up a small example using ideas that are familiar from Rapid. This is not a Rapid guide though, so we’ll define these tags from scratch. First off, the <code>&lt;card&gt;</code> tag. This captures the very common pattern of web pages displaying collections of some kind of object as small “cards”: comments, friends, discussion threads, etc.</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"card"</span> <span>merge-attrs</span>&gt;
&lt;<span>h3 </span><span>param</span>=<span>"heading"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>h3</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"body"</span>&gt;&lt;/<span>div</span>&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We’ve defined a very simple <code>&lt;card&gt;</code> that uses the <code>to_s</code> method to give a default heading, and provides a <code>&lt;body:&gt;</code> parameter that is blank by default. Here’s how we might use it:</p>
<pre><code>&lt;<span>h2</span>&gt;Discussions&lt;/<span>h2</span>&gt;
&lt;<span>ul</span>&gt;
&lt;<span>li </span><span>repeat</span>=<span>"@discussions"</span>&gt;
&lt;<span>card</span>&gt;
&lt;<span>body:</span>&gt;&lt;%= this.posts.length %&gt; posts&lt;/<span>body:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>li</span>&gt;
&lt;/<span>ul</span>&gt;</code></pre>
<p>This example (specifically, the collection created in the <code>&lt;li repeat="@discussions"&gt;</code> section) demonstrates that as soon as we have the concept of a card, we very often find ourselves wanting to render a collection of <code>&lt;card&gt;</code> tags. The obvious next step is to capture that collection-of-cards idea as a reusable tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"collection"</span>&gt;
&lt;<span>h2 </span><span>param</span>=<span>"heading"</span>&gt;&lt;/<span>h2</span>&gt;
&lt;<span>ul</span>&gt;
&lt;<span>li </span><span>repeat</span>&gt;
&lt;<span>card </span><span>param</span>&gt;
&lt;/<span>li</span>&gt;
&lt;/<span>ul</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>The <code>&lt;collection&gt;</code> tag has a straightforward <code>&lt;heading:&gt;</code> parameter, but notice that the <code>&lt;card&gt;</code> tag is also declared as a parameter. Whenever you add <code>param</code> to a tag that itself also has parameters, you give your “super-tag” (<code>&lt;collection&gt;</code> in this case) the ability to customise the “sub-tag” (<code>&lt;card&gt;</code> in this case) using <em>nested parameters</em>. Here’s how we can use the nested parameters in the <code>&lt;collection&gt;</code> tag to get the same output as the <code>&lt;li repeat="@discussions"&gt;</code> section in the previous example:</p>
<pre><code>&lt;<span>collection</span>&gt;
&lt;<span>heading:</span>&gt;Discussions&lt;/<span>heading:</span>&gt;
&lt;<span>card:</span>&gt;&lt;<span>body:</span>&gt;&lt;%= this.posts.length %&gt; posts&lt;/<span>body:</span>&gt;&lt;/<span>card:</span>&gt;
&lt;/<span>collection</span>&gt;</code></pre>
<p>This nesting works to any depth. To show this, if we define an <code>&lt;index-page&gt;</code> tag that uses <code>&lt;collection&gt;</code> and declares it as a paramater:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"index-page"</span>&gt;
&lt;<span>html</span>&gt;
&lt;<span>head</span>&gt; ... &lt;/<span>head</span>&gt;
&lt;<span>body</span>&gt;
&lt;<span>h1 </span><span>param</span>=<span>"heading"</span>&gt;&lt;/<span>h1</span>&gt;
...
&lt;<span>collection </span><span>param</span>&gt;
...
&lt;/<span>body</span>&gt;
&lt;/<span>html</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>we can still access the card inside the collection inside the page:</p>
<pre><code>&lt;<span>index-page</span>&gt;
&lt;<span>heading:</span>&gt;Welcome to our forum&lt;/<span>heading:</span>&gt;
&lt;<span>collection:</span>&gt;
&lt;<span>heading:</span>&gt;Discussions&lt;/<span>heading:</span>&gt;
&lt;<span>card:</span>&gt;&lt;<span>body:</span>&gt;&lt;%= this.posts.length %&gt; posts&lt;/<span>body:</span>&gt;&lt;/<span>card:</span>&gt;
&lt;/<span>collection:</span>&gt;
&lt;/<span>index-page</span>&gt;</code></pre>
<p>Pay careful attention to the use of the trailing ’:’. The definition of <code>&lt;index-page&gt;</code> contains a <em>call</em> the collection tag, written <code>&lt;collection&gt;</code> (no ’:’). By contrast, the above call to <code>&lt;index-page&gt;</code> <em>customises</em> the call to the collection tag that is already present inside <code>&lt;index-page&gt;</code>, so we write <code>&lt;collection:&gt;</code> (with a ’:’). Remember:</p>
<ul>
<li>Without ’:’ &ndash; call a tag</li>
<li>With ’:’ &ndash; customise an existing call inside the definition</li>
</ul>
<h1 id="customising_and_extending_tags">Customising and extending tags</h1>
<p>As we’ve seen, DRYML makes it easy to define tags that are highly customisable. By adding <code>param</code>s to the tags inside your definition, the caller can insert, replace and tweak to their heart’s content. Sometimes the changes you make to a tag’s output are needed not once, but many times throughout the site. In other words, you want to define a new tag in terms of an existing tag.</p>
<h2 id="new_tags_from_old">New tags from old</h2>
<p>As an example, let’s bring back our card tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"card"</span> <span>merge-attrs</span>&gt;
&lt;<span>h3 </span><span>param</span>=<span>"heading"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>h3</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"body"</span>&gt;&lt;/<span>div</span>&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now let’s say we want a new kind of card, one that has a link to the resource that it represents. Rather than redefine the whole thing from scratch, we can define the new card, say, “linked-card”, like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"linked-card"</span>&gt;
&lt;<span>card</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>That’s all well and good but there are a couple of problems:</p>
<ul>
<li>The original card used <code>merge-attrs</code> so that we could add arbitrary HTML attributes to the final <code>&lt;div&gt;</code>. Our new card has lost that feature</li>
<li>Worse than that, the new card is in fact useless, as there’s no way to pass it the body parameter</li>
</ul>
<p>Let’s solve those problems in turn. First the attributes.</p>
<h2 id="_again"><code>merge-attrs</code> again</h2>
<p>In fact <code>merge-attrs</code> works just the same on defined tags as it does on HTML tags that are output, so we can simply add it to the call to <code>&lt;card&gt;</code>, like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"linked-card"</span>&gt;
&lt;<span>card </span><span>merge-attrs</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Now we can do things like <code>&lt;linked-card&gt;</code>, and the attribute will be passed from <code>&lt;linked-card&gt;</code>, to <code>&lt;card&gt;</code>, to the rendered <code>&lt;div&gt;</code>.</p>
<p>Now we’ll fix the parameters, it’s going to look somewhat similar…</p>
<h2 id="id92"><code>merge-params</code></h2>
<p>We’ll introduce <code>merge-params</code> the same way we introduced <code>merge-attrs</code> &ndash; by showing how you would get by without it. The problem with our <code>&lt;linked-card&gt;</code> tag is that we’ve lost the <code>&lt;body:&gt;</code> parameter. We could bring it back like this:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"linked-card"</span>&gt;
&lt;<span>card </span><span>merge-attrs</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;<span>body:</span> <span>param</span>/&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>In other words, we use the <code>param</code> declaration to give <code>&lt;linked-card&gt;</code> a <code>&lt;body:&gt;</code> parameter, which is forwarded to <code>&lt;card&gt;</code>. But what if <code>&lt;card&gt;</code> had several parameters? We would have to list them all out. And what if we add a new parameter to <code>&lt;card&gt;</code> later? We would have to remember to update <code>&lt;linked-card&gt;</code> and any other customised cards we had defined.</p>
<p>Instead we use <code>merge-params</code>, much as we use <code>merge-attrs</code>:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"linked-card"</span>&gt;
&lt;<span>card </span>merge-attrs <span>merge-params</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>You can read <code>merge-params</code> as: take any “extra” parameters passed to <code>&lt;linked-card&gt;</code> and forward them all to <code>&lt;card&gt;</code>. By “extra” parameters, we mean any that are not declared as parameters (via the <code>param</code> attribute) inside the definition of <code>&lt;linked-card&gt;</code>.</p>
<p>There are two local variables inside the tag definition that mirror the <code>attributes</code> and <code>all_attributes</code> variables described previously:</p>
<ul>
<li><code>parameters</code> a hash containing all the “extra” parameters (those that do not match a declared parameter name)</li>
<li><code>all_parameters</code> a hash containing all the parameters passed to the tag</li>
</ul>
<p>The values in these hashes are Ruby procs. One common use of <code>all_parameters</code> is to test if a certain parameter was passed or not:</p>
<pre><code>&lt;<span>if </span><span>test</span>=<span>"&amp;all_parameters[:body]"</span>&gt;</code></pre>
<p>In fact, <code>all_parameters</code> and <code>parameters</code> are not regular hashes, they are instances of a subclass of <code>Hash</code> &ndash; <code>Hobo::Dryml::TagParameters</code>. This subclass allows parameters to be called as if they were methods on the hash object, e.g.:</p>
<pre><code>parameters.default</code></pre>
<p>That’s not something you’ll use often.</p>
<h2 id="id93"><code>merge</code></h2>
<p>As it’s very common to want both <code>merge-attrs</code> and <code>merge-params</code> on the same tag, there is a shorthand for this: <code>merge</code>. So the final, preferred definition of <code>&lt;linked-card&gt;</code> is:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"linked-card"</span>&gt;
&lt;<span>card </span><span>merge</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<h2 id="merging_selected_parameters">Merging selected parameters</h2>
<p>Just as with <code>merge-attrs</code>, <code>merge-params</code> can be given a value &#8211; either a hash containing the parameters you wish to merge, or a list of parameter names (comma separated), to be merged from the <code>all_parameters</code> variable.</p>
<p>Examples:</p>
<pre><code>&lt;<span>card </span><span>merge-params</span>=<span>"heading, body"</span>&gt;

&lt;<span>card </span><span>merge-params</span>=<span>"&amp;my_parameter_hash"</span>&gt;</code></pre>
<h2 id="extending_a_tag">Extending a tag</h2>
<p>We’ve now seen how to easily create a new tag from an existing tag. But what if we don’t actually want a new tag, but rather we want to change the behaviour of an existing tag in some way, and keep the tag name the same. What we can’t do is simply use the existing name in the definition:</p>
<pre><code><span>&lt;!-- DOESN'T WORK! --&gt;</span>
&lt;<span>def </span><span>tag</span>=<span>"card"</span>&gt;
&lt;<span>card </span><span>merge</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>All we’ve done there is created a nice stack overflow when the card calls itself over and over.</p>
<p>Fortunately, DRYML has support for extending tags. Use <code>&lt;extend&gt;</code> instead of <code>&lt;def&gt;</code>:</p>
<pre><code>&lt;<span>extend </span><span>tag</span>=<span>"card"</span>&gt;
&lt;<span>old-card </span><span>merge</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"&amp;object_url this"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>old-card</span>&gt;
&lt;/<span>extend</span>&gt;</code></pre>
<p>The one thing to notice there is that the “old” version of <code>&lt;card&gt;</code>, i.e. the one that was active before you’re extension, is available as <code>&lt;old-card&gt;</code>. That’s about all there is to it. <code>&lt;old-card&gt;</code> is only available inside of <code>&lt;extend&gt;</code> &ndash; if you need it elsewhere you can alias the old card <em>before</em> you extend it. (See the next chapter).</p>
<p>Here’s another example where we add a footer to every page in our application. It’s very common to <code>&lt;extend tag="page"&gt;</code> in your application.dryml, in order to make changes that should appear on every page:</p>
<pre><code>&lt;<span>extend </span><span>tag</span>=<span>"page"</span>&gt;
&lt;<span>old-page </span><span>merge</span>&gt;
&lt;<span>footer:</span> <span>param</span>&gt;
... your custom footer here ...
&lt;/<span>footer:</span>&gt;
&lt;/<span>old-page</span>&gt;
&lt;/<span>extend</span>&gt;</code></pre>
<h1 id="aliasing_tags">Aliasing tags</h1>
<p>Welcome to the shortest chapter of The DRYML Guide.</p>
<p>If you want to create an alias of a tag; i.e., an identical tag with a different name:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"my-card"</span> <span>alias-of</span>=<span>"card"</span>/&gt;</code></pre>
<p>Note that’s a self closing tag &ndash; there is no body to the definition.</p>
<p>So… that’s aliasing tags then…</p>
<h1 id="polymorphic_tags">Polymorphic tags</h1>
<p>DRYML allows you to define a whole collection of tags that share the same name, where each definition is appropriate for a particular type of object being rendered. When you call the tag, the type (i.e. class) of the context is used to determine which definition to call. These are called polymorphic tags.</p>
<p>To illustrate how these work, let’s bring back our simple <code>&lt;card&gt;</code> tag once more.</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span> <span>polymorphic</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"card"</span> <span>merge-attrs</span>&gt;
&lt;<span>h3 </span><span>param</span>=<span>"heading"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>h3</span>&gt;
&lt;<span>div </span><span>param</span>=<span>"body"</span>&gt;&lt;/<span>div</span>&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We’ve added the <code>polymorphic</code> attribute to the <code>&lt;def&gt;</code>. This tells DRYML that <code>&lt;card&gt;</code> can have many definitions, each for a particular type. The definition we’ve given here is called the “base” definition or the “base card”. The base definition serves two purposes:</p>
<ul>
<li>It is the fallback if we call <code>&lt;card&gt;</code> and no definition is found for the current type.</li>
<li>The type-specific definition can use the base definition as a starting point to be further customised.</li>
</ul>
<p>To add a type-specific <code>&lt;card&gt;</code>, we use the <code>for</code> attribute on the <code>&lt;def&gt;</code>. For example, a card for a <code>Product</code>:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span> <span>for</span>=<span>"Product"</span>&gt;
...
&lt;/<span>def</span>&gt;</code></pre>
<p>(Note: if the name in the <code>for</code> attribute starts with an uppercase letter, is is taken to be a class name. Otherwise it is taken to be an abbreviated name registered with HoboFields; e.g., <code>&lt;def tag="input" for="email_address"&gt;</code>)</p>
<p>For the product card, lets make the heading be a link to the product, and put the price of the product in the body area:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span> <span>for</span>=<span>"Product"</span>&gt;
&lt;<span>card </span><span>merge</span>&gt;
&lt;<span>heading:</span> <span>param</span>&gt;&lt;<span>a </span><span>href</span>=<span>"#{object_url this}"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;<span>body:</span> <span>param</span>=<span>"price"</span>&gt;$&lt;%= this.price %&gt;&lt;/<span>body:</span>&gt;
&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>We call this a type-specific definition. Some points to notice:</p>
<ul>
<li>The callback to <code>&lt;card&gt;</code> is not a recursive loop, but a call to the base definition.</li>
<li>We’re using the normal technique for customising / extending an existing card; i.e., we’re using <code>merge</code>.</li>
</ul>
<p>It is not required for the type-specific definition to call the base definition, it’s just often convenient. In fact the base definition is not required. It is valid to declare a polymorphic tag with no content:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"my-tag"</span> <span>polymorphic</span>/&gt;</code></pre>
<h2 id="type_hierarchy">Type hierarchy</h2>
<p>If, for a given call, no type-specific definition is available for <code>this.class</code>, the search continues with <code>this.class.superclass</code> and so on up the superclass chain. If the search reaches either <code>ActiveRecord::Base</code> or <code>Object</code>, the base definition is used.</p>
<h2 id="specifying_the_type_explicitly">Specifying the type explicitly</h2>
<p>Sometimes it is useful to give the type explicitly for the call explicitly (i.e., to override the use of <code>this.class</code>). The <code>for-type</code> attribute (on the call) provides this facility. For example, you might want to implement one type-specific definition in terms of another:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"card"</span> <span>for</span>=<span>"SpecialProduct"</span>&gt;
&lt;<span>card </span><span>for-type</span>=<span>"Product"</span>&gt;&lt;<span>append-price:</span>&gt; (Today Only!)&lt;/<span>append-price:</span>&gt;&lt;/<span>card</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<h2 id="extending_polymorphic_tags">Extending polymorphic tags</h2>
<p>Type-specific definitions can be extended just like any other tag using the <code>&lt;extend&gt;</code> tag. For example, here we simply remove the price:</p>
<pre><code>&lt;<span>extend </span><span>tag</span>=<span>"card"</span> <span>for</span>=<span>"Product"</span>&gt;
&lt;<span>old-card </span>merge <span>without-price</span>/&gt;
&lt;/<span>extend</span>&gt;</code></pre>
<h1 id="wrapping_content">Wrapping content</h1>
<p>DRYML provides two mechanism for wrapping existing content inside new tags.</p>
<h2 id="wrapping_inside_a_parameter">Wrapping <em>inside</em> a parameter</h2>
<p>Once or twice in the previous examples, we have extended our card tag definition, replacing the plain heading with a hyperlink heading. Here is an example call to our extended card tag:</p>
<pre><code>&lt;<span>card</span>&gt;
&lt;<span>heading:</span>&gt;&lt;<span>a </span><span>href</span>=<span>"#{object_url this}"</span>&gt;&lt;%= h this.to_s %&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;</code></pre>
<p>There’s a bit of repetition there &ndash; <code>&lt;%= h this.to_s %&gt;</code> was already present in the original definition. All we really wanted to do was wrap the existing heading in an <code>&lt;a&gt;</code>. In this case there wasn’t much markup to repeat, so it wasn’t a big deal, but in other cases there might be much more.</p>
<p>We can’t use <code>&lt;prepend-heading:&gt;&lt;a&gt;&lt;/prepend-heading:&gt;</code> and <code>&lt;append-heading:&gt;&lt;/a&gt;&lt;/append-heading:&gt;</code> because that’s not well formed markup (and is very messy besides). Instead, DRYML has a specific feature for this situation. The <code>&lt;param-content&gt;</code> tag is a special tag that brings back the default content for a parameter. Here’s how it works:</p>
<pre><code>&lt;<span>card</span>&gt;
&lt;<span>heading:</span>&gt;&lt;<span>a </span><span>href</span>=<span>"#{object_url this}"</span>&gt;&lt;<span>param-content </span><span>for</span>=<span>"heading"</span>/&gt;&lt;/<span>a</span>&gt;&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;</code></pre>
<p>That’s the correct way to wrap <em>inside</em> the parameter, so in this case the output is:</p>
<pre><code>&lt;h3&gt;&lt;a href="..."&gt;Fried Bananas&lt;/a&gt;&lt;/h3&gt;</code></pre>
<p>What if we wanted to wrap the <em>entire</em> <code>&lt;heading:&gt;</code> parameter, including the <code>&lt;h3&gt;</code> tags?</p>
<h2 id="wrapping_outside_a_parameter">Wrapping <em>outside</em> a parameter</h2>
<p>For example, we might want to give the card a new ‘header’ section, that contained the heading, and the time the record was created, like this:</p>
<pre><code>&lt;<span>div </span><span>class</span>=<span>"header"</span>&gt;
&lt;<span>h3</span>&gt;Fried Bananas&lt;/<span>h3</span>&gt;
&lt;<span>p</span>&gt;Created: ....&lt;/<span>p</span>&gt;
&lt;/<span>div</span>&gt;</code></pre>
<p>To use DRYML terminology, what we’ve done there is <em>replaced</em> the entire heading with some new content, and the new content happens to contain the original heading. So we replaced the heading, and then restored it again, which in DRYML is written:</p>
<pre><code>&lt;<span>card</span>&gt;
&lt;<span>heading:</span> <span>replace</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"header"</span>&gt;
&lt;<span>heading:</span> <span>restore</span>/&gt;
&lt;<span>p</span>&gt;Created: &lt;%= this.created_at.to_s(:short) %&gt;&lt;/<span>p</span>&gt;
&lt;/<span>div</span>&gt;
&lt;/<span>heading:</span>&gt;
&lt;/<span>card</span>&gt;</code></pre>
<p>To summarise, to wrap content inside a parameter, use <code>&lt;param-content/&gt;</code>. To wrap an entire parameter, including the parameterised tag itself (the <code>&lt;h3&gt;</code> in our examples), use the <code>replace</code> and <code>restore</code> attributes.</p>
<h1 id="local_variables_and_scoped_variables">Local variables and scoped variables.</h1>
<p>DRYML provides two tags for setting variables: <code>&lt;set&gt;</code> and <code>&lt;set-scoped&gt;</code>.</p>
<h2 id="setting_local_variables_with_">Setting local variables with <code>&lt;set&gt;</code></h2>
<p>Sometimes it’s useful to define a local variable inside a template or a tag definition. It’s worth avoiding if you can, as we don’t really want our view layer to contain lots of low-level code, but sometimes it’s unavoidable. Because DRYML extends ERB, you can simply write:</p>
<pre><code>&lt;% total = price_of_fish * number_of_fish %&gt;</code></pre>
<p>For purely aesthetic reasons, DRYML provides a tag that does the same thing:</p>
<pre><code>&lt;<span>set </span><span>total</span>=<span>"&amp;price_of_fish * number_of_fish"</span>/&gt;</code></pre>
<p>Note that you can put as many attribute/value pairs as you like on the same <code>&lt;set&gt;</code> tag, but the order of evaluation is not defined.</p>
<h2 id="scoped_variables__">Scoped variables &ndash; <code>&lt;set-scoped&gt;</code></h2>
<p>Scoped variables (which is not a great name, I realise as I come to document them properly) are kind of like global variables with a limited lifespan. We all know the pitfalls of global variables, and DRYML’s scoped variables should indeed be used as sparingly as possible, but you can pull off some very useful tricks with them.</p>
<p>The <code>&lt;set-scoped&gt;</code> tag is very much like <code>&lt;set&gt;</code> except you open it up and put DRYML inside it:</p>
<pre><code>&lt;<span>set-scoped </span><span>xyz</span>=<span>"&amp;..."</span>&gt;
...
&lt;/<span>set-scoped</span>&gt;</code></pre>
<p>The value is available as <code>scope.xyz</code> anywhere inside the tag <em>and in any tags that are called inside that tag</em>. That’s the difference between <code>&lt;set&gt;</code> and <code>&lt;set-scoped&gt;</code>. They are like <em>dynamic variables</em> from LISP. To repeat the point, they are like global variables that exist from the time the <code>&lt;set-scope&gt;</code> tag is evaluated, and for the duration of the evaluation of the body of the tag, and are then removed.</p>
<p>As an example of their use, let’s define a simple tag for rendering navigation links. The output should be a list of <code>&lt;a&gt;</code> tags, and the <code>&lt;a&gt;</code> that represents the “current” page should have a CSS class “current”, so it can be highlighted in some way by the stylesheet. (In fact, the need to create a reusable tag like this is where the feature originally came from).</p>
<p>On our pages, we’d like to simply call, say:</p>
<pre><code>`&lt;<span>main-nav </span><span>current</span>=<span>"Home"</span>&gt;`</code></pre>
<p>And we’d like it to be easy to define our own <code>&lt;main-nav&gt;</code> tag in our applications:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"main-nav"</span>&gt;
&lt;<span>navigation </span><span>merge-attrs</span>&gt;
&lt;<span>nav-item </span><span>href</span>=<span>"..."</span>&gt;Home&lt;/<span>nav-item</span>&gt;
&lt;<span>nav-item </span><span>href</span>=<span>"..."</span>&gt;News&lt;/<span>nav-item</span>&gt;
&lt;<span>nav-item </span><span>href</span>=<span>"..."</span>&gt;Offers&lt;/<span>nav-item</span>&gt;
&lt;/<span>navigation</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>Here’s the definition for the <code>&lt;navigation&gt;</code> tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"navigation"</span> <span>attrs</span>=<span>"current"</span>&gt;
&lt;<span>set-scoped </span><span>current-nav-item</span>=<span>"current"</span>&gt;
&lt;<span>ul </span>merge-attrs <span>param</span>=<span>"default"</span>/&gt;
&lt;/<span>set-scoped
</span>&lt;/<span>def</span>&gt;</code></pre>
<p>All <code>&lt;navigation&gt;</code> does is set a scoped-variable to whatever was given as <code>current</code> and output the body wrapped in a <code>&lt;ul&gt;</code>.</p>
<p>Here’s the definition for the <code>&lt;nav-item&gt;</code> tag:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"nav-item"</span>&gt;
&lt;<span>set </span><span>body</span>=<span>"&amp;parameters.default"</span>/&gt;
&lt;<span>li </span><span>class</span>=<span>"#{'current' if scope.current_nav_item == body}"</span>&gt;
&lt;<span>a </span><span>merge-attrs</span>&gt;&lt;%= body %&gt;
&lt;/<span>li</span>&gt;
&lt;/<span>def</span>&gt;</code></pre>
<p>The content inside the <code>&lt;nav-item&gt;</code> is compared to <code>scope.current_nav_item</code>. If they are the same, the “current” class is added. Also note the way <code>parameters.default</code> is evaluated and the result stored in the local variable <code>body</code>, in order to avoid evaluating the body twice.</p>
<h3 id="nested_scopes">Nested scopes</h3>
<p>One of the strengths of scoped variables is that scopes can be nested, and where there are name clashes, the parent scope variable is temporarily hidden, rather than overwritten. With a bit of tweaking, we could use this fact to extend our <code>&lt;navigation&gt;</code> tag to support a sub-menu of links within a top level section. The sub-menu could also use <code>&lt;navigation&gt;</code> and <code>&lt;nav-item&gt;</code> and the two <code>scope.current_nav_item</code> variables would not conflict with each other.</p>
<h1 id="taglibs">Taglibs</h1>
<p>DRYML provides the <code>&lt;include&gt;</code> tag to support breaking up lots of tag definitions into separate “tag libraries”, known as taglibs. You can call <code>&lt;include&gt;</code> with several different formats:</p>
<pre><code>&lt;<span>include </span><span>src</span>=<span>"foo"</span>/&gt;</code></pre>
<p>Load <code>foo.dryml</code> from the same directory as the current template or taglib.</p>
<pre><code>&lt;<span>include </span><span>src</span>=<span>"path/to/foo"</span>/&gt;</code></pre>
<p>Load <code>app/views/path/to/foo.dryml</code></p>
<pre><code>&lt;<span>include </span><span>src</span>=<span>"foo"</span> <span>plugin</span>=<span>"path/to/plugin"</span>/&gt;</code></pre>
<p>Load <code>vendor/plugins/path/to/plugin/taglibs/foo.dryml</code></p>
<p>When running in development mode, all of these libraries are automatically reloaded on every request.</p>
<h1 id="divergences_from_xml_and_html">Divergences from XML and HTML</h1>
<h2 id="selfclosing_tags">Self-closing tags</h2>
<p>In DRYML, <code>&lt;foo:/&gt;</code> and <code>&lt;foo:&gt;&lt;/foo:&gt;</code> have two slightly different meanings.</p>
<p>The second form replaces the parameter’s default inner content with the specified content: nothing in this case.</p>
<p>The first form uses the parameters default inner content unchanged.</p>
<p>This is very useful if you wish to add an attribute to a parameter but leave the inner content unchanged. In this example:</p>
<pre><code>&lt;<span>def </span><span>tag</span>=<span>"bar"</span>&gt;
&lt;<span>div </span><span>class</span>=<span>"container"</span> <span>merge-attrs</span>&gt;
&lt;<span>p </span><span>class</span>=<span>"content"</span> <span>param</span>&gt;
Hello
&lt;/<span>p</span>&gt;
&lt;/<span>div</span>&gt;
&lt;<span>def</span>&gt;

&lt;<span>bar</span>&gt;&lt;<span>foo:</span> <span>class</span>=<span>"my-foo"</span>/&gt;&lt;/<span>bar</span>&gt;</code></pre>
<p>gives</p>
<pre><code>&lt;<span>div </span><span>class</span>=<span>"container"</span>&gt;
&lt;<span>p </span><span>class</span>=<span>"content my-foo"</span>&gt;
Hello
&lt;/<span>p</span>&gt;
&lt;/<span>div</span>&gt;</code></pre>
<p>If you used</p>
<pre><code>&lt;<span>bar</span>&gt;&lt;<span>foo:</span> <span>class</span>=<span>"my-foo"</span>&gt;&lt;/<span>foo:</span>&gt;&lt;/<span>bar</span>&gt;</code></pre>
<p>You would get</p>
<pre><code>&lt;<span>div </span><span>class</span>=<span>"container"</span>&gt;
&lt;<span>p </span><span>class</span>=<span>"content my-foo"</span>&gt;&lt;/<span>p</span>&gt;
&lt;/<span>div</span>&gt;</code></pre>
<h2 id="colons_in_tag_names">Colons in tag names</h2>
<p>In XML, colons are valid inside tag and attribute names. However they are reserved for “experiments for namespaces”. So it’s possible that we may be non-compliant with the not-yet-existent XML 2.0.</p>
<h2 id="close_tag_shortcuts">Close tag shortcuts</h2>
<p>In DRYML, you’re allowed to close tags with everything preceding the colon:</p>
<pre><code>&lt;<span>view</span><span>:name</span>&gt; Hello &lt;/<span>view</span>&gt;</code></pre>
<p>XML requires the full tag to be specified:</p>
<pre><code>&lt;<span>view</span><span>:name</span>&gt; Hello &lt;/<span>view</span><span>:name</span>&gt;</code></pre>
<h2 id="null_end_tags">Null end tags</h2>
<p>Self-closing tags are <a href="http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.2.1">technically illegal</a> in HTML. So <code>&lt;br /&gt;</code> is technically not valid HTML. However, browsers do parse it as you expect. It is valid XHTML, though.</p>
<p>However, browsers only do this for <em>empty</em> elements. So tags such as <code>&lt;script&gt;</code> and <code>&lt;a&gt;</code> require a separate closing tag in HTML. This behaviour has surprised many people. <code>&lt;script src="foobar.js" /&gt;</code> is not recognized in many web browsers for this reason. You must use <code>&lt;script src="foorbar.js"&gt;&lt;/script&gt;</code> in HTML instead.</p>
<p>DRYML follows the XML conventions. <code>&lt;a/&gt;</code> is valid DRYML.</p>
<p>That’s all folks!</p>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/129/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=129&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/04/16/what-is-dryml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>rails deligation for associations</title>
		<link>http://shivsingh.wordpress.com/2010/03/14/rails-deligation-for-associations/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/14/rails-deligation-for-associations/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 03:38:14 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[associaions]]></category>
		<category><![CDATA[deligation]]></category>
		<category><![CDATA[dot-magic]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[rails activerecord]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=126</guid>
		<description><![CDATA[In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/14/rails-deligation-for-associations/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=126&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<div>
<p>In ruby on rails Delegation provide us a fine way over dot-magic. Using delegation it is easy to access associated object’s attributes. Let take an example-</p>
<p>For example we have a two models named User and profile having has-one association.</p>
<p><strong><em>Class User &lt; ActiveRecord::Base</em></strong></p>
<p><strong><em> has_one :profile</em></strong></p>
<p><strong><em>end</em></strong></p>
<p>&amp;</p>
<p><strong><em>Class Profile &lt; ActiveRecord::Base</em></strong></p>
<p><strong><em> belongs_to  :user</em></strong></p>
<p><strong><em>end</em></strong></p>
<p>Suppose Profile have attributes like first_name, last_name, phone etc and @user is the instance of User class.</p>
<p>now to find name or phone of an user we have two ways -</p>
<p>1. Using dot-magic</p>
<p><strong><em>FirstName = @user.profile.first_name</em></strong></p>
<p>This is simple but becomes bulky for long chain associated object.For example @user.sth.sth.sth.sth…some_attribute</p>
<p>2. Using delegation</p>
<p>Instead we can delegate desired attributes in User model. Here is the code to add</p>
<p><strong><em>Class User &lt; ActiveRecord::Base</em></strong></p>
<p><strong><em> has_one :profile</em></strong></p>
<p><strong><em> [:first_name, :last_name, :phone, ...etc.].each {|attr| delegate attr,  :to =&gt; :profile}</em></strong></p>
<p><strong><em>end</em></strong></p>
<p>Now we can access first name of an user with <em>@user.first_name</em> instead of <em>@user.profile.first_name</em>.</p>
<p>We can also specify prefix like profile_first_name, just need to pass it .</p>
<p><strong><em>delegate :firstname, :to =&gt; :profile, :prefix =&gt; true</em></strong></p>
<p>now @user object can access first_name attribute by <em>@user.profile_first_name</em></p>
<p>If we want some different prefix then we just need to pass it instead of true.</p>
<p><strong><em>delegate :firstname, :to =&gt; :profile, :prefix =&gt; :info</em></strong></p>
<p>now @user object can access first_name attribute by <em>@user.info_first_name.</em></p>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=126&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/14/rails-deligation-for-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>Git &#8211; basic</title>
		<link>http://shivsingh.wordpress.com/2010/03/14/git-basic/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/14/git-basic/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 03:29:57 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Help]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[git commands]]></category>
		<category><![CDATA[git tutorial]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=124</guid>
		<description><![CDATA[These are the simple steps to create a new branch on your git repository and merge it with master branch. &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/14/git-basic/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=124&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<div>
<p>These are the simple steps to create a new branch on your git repository and merge it with master branch.</p>
<p>1. Create a local branch<br />
<strong>git checkout -b new_branch_name</strong></p>
<p>2. Create a remote branch<br />
<strong>git push origin master : refs/heads/new_branch_name</strong></p>
<p>3. Switch to master branch<br />
<strong>git checkout master</strong></p>
<p>4. Delete the local branch just created<br />
<strong>git branch -d new_branch_name</strong></p>
<p>5. Track the newly created remote branch<br />
<em>please read dash-dash-track in the below line</em><br />
<strong>git checkout –track -b new_branch_name origin/new_branch_name</strong><br />
now the new branch is setup. you can now make the changes into project code, commit and push it to this new branch.</p>
<p>To merge this new branch with master branch, go through the following steps</p>
<p>6. Switch to master branch<br />
<strong>git checkout master</strong></p>
<p>7. Update for changes on master branch<br />
<strong>git pull origin master(or only <em>git pull</em> )</strong></p>
<p>8. Merge the new branch into master branch<br />
<strong>git merge new_branch_name</strong></p>
<p>9. Now finally push the code to remote master branch<br />
<strong>git push origin master(or only <em>git push</em> )</strong></p>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=124&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/14/git-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>Some General Knowledge about XML</title>
		<link>http://shivsingh.wordpress.com/2010/03/02/some-general-knowledge-about-xml/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/02/some-general-knowledge-about-xml/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 13:13:38 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[web 2.0 news]]></category>
		<category><![CDATA[BML]]></category>
		<category><![CDATA[CML]]></category>
		<category><![CDATA[FIXML]]></category>
		<category><![CDATA[General Knowledge]]></category>
		<category><![CDATA[MATHML]]></category>
		<category><![CDATA[MRMML]]></category>
		<category><![CDATA[MUSICML]]></category>
		<category><![CDATA[OFX]]></category>
		<category><![CDATA[P3P]]></category>
		<category><![CDATA[RELML]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[SMIL]]></category>
		<category><![CDATA[VoxML]]></category>
		<category><![CDATA[WML]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XML GK]]></category>
		<category><![CDATA[XML info]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=120</guid>
		<description><![CDATA[XML If you know of other schema&#8217;s please feel free to send and E-mail to us, if possible please include &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/02/some-general-knowledge-about-xml/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=120&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><span style="font-family:Old English Text MT;color:#00ff00;font-size:x-large;"><em> XML</em></span></p>
<p>If you know of other schema&#8217;s please feel free to send and E-mail      to us, if possible please include a URL for the site that is developing it      or has more information on it.</p>
<p><span style="font-size:small;"><strong>Schema Schemes</strong></span></p>
<p><strong><span style="font-size:small;">XML is a fundamental tool for the growth of      business-to business e-commerce. Part of its power comes from its ability      to energize the information exchange between companies. While use of XML to      glue together business-to business partnerships is growing, another larger      trend is emerging. Entire industries are building XML languages, or schemas,      that will simplify communications and business processes between partners      in those sectors. A year ago, only a handful of industry trade associations      were considering adopting XML as the Rosetta stone for business-to-business      e-commerce. Today an explosion of XML schemas has appeared on the landscape.      Here&#8217;s a survey of some of the more important schemas being developed.</span></strong></p>
<p><strong><span style="font-size:small;"><em><span style="color:#00ff00;">BML (Bean markup      Language)</span></em> &#8211; An XML schema for access and configuration of JavaBeans</span></strong></p>
<p><strong><span style="font-size:small;"><em><span style="color:#00ff00;">CML (Chemical Markup      Language)</span></em> &#8211; For graphic rendering of the molecular structure of      chemical compounds.</span></strong></p>
<p><strong><em><span style="color:#00ff00;">FIXML (Financial Information eXchange)</span></em> &#8211; for the exchange of real-time stock trading information.</strong></p>
<p><strong><span style="color:#00ff00;"><em>MRMML (Human Resource Management      Markup Language)</em></span> &#8211; for the representation of resumes and job postings.</strong></p>
<p><strong><span style="color:#00ff00;"><em>MATHML (MATHematical Markup Language)</em></span> &#8211; for desplay and presentation of mathematical equations and fromulas.</strong></p>
<p><strong><span style="color:#00ff00;"><em>MUSICML (MUSIC Markup Language)</em></span> &#8211; for publishing sheet music on the WEB.</strong></p>
<p><strong><span style="color:#00ff00;"><em>OFX (Open Financial eXchange)</em></span> &#8211; for exchange of financial data between financial institutions, businesses,      and customers.</strong></p>
<p><strong> <em><span style="color:#00ff00;">P3P (Platfrom for Privacy Preferences)</span></em> &#8211; for exchanging privacy information between two organizations or individuals.</strong></p>
<p><strong><span style="color:#00ff00;"><em>RELML (Real Estate Listing Markup      Language)</em></span> &#8211; for presenting and exchanging real estate listing over      the internet RosettaNet, </strong></p>
<p><strong><span style="color:#00ff00;"><em>SMIL (Synchronized Multimedia Integration      Language)</em></span> &#8211; that allows synchronization of many multimedia sources      in a single WEB presentation.</strong></p>
<p><strong><em><span style="color:#00ff00;">VoxML (Voice Recongnition Markup      Language)</span></em> &#8211; defines how WEB content is accessd and prsented by      voice recognition software.</strong></p>
<p><strong><em><span style="color:#00ff00;">WML (Wireless Markup Language)</span></em> &#8211; defines how WEB content is accessed and presented by small, handheld devices</strong></p>
<p><span style="font-size:xx-small;"><a href="mailto:ggibat@gibat.com"></a></span></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/120/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/120/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/120/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=120&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/02/some-general-knowledge-about-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>Rails Logger &#8211; filter password in log file</title>
		<link>http://shivsingh.wordpress.com/2010/03/01/rails-logger-filter-password-in-log-file/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/01/rails-logger-filter-password-in-log-file/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 09:34:20 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[filter pasword]]></category>
		<category><![CDATA[log file]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[password in log]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=114</guid>
		<description><![CDATA[Rails logger is an interesting chapter. Many a times we don't worry about logger operations at all. But do you &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/01/rails-logger-filter-password-in-log-file/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=114&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<pre>Rails logger is an interesting chapter. Many a times we don't worry about logger operations at all.
But do you know logger records password of every user in it. If the site users passwords are confidential
in your site, then you must include the following filter code in your application controller.
This line of code will not allow password to appear in your log file. Enjoy <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 

class ApplicationController &lt; ActionController::Base
 filter_parameter_logging :password
end</pre>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=114&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/01/rails-logger-filter-password-in-log-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>ruby &#8211; EventMachine is rocking</title>
		<link>http://shivsingh.wordpress.com/2010/03/01/ruby-eventmachine-is-rocking/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/01/ruby-eventmachine-is-rocking/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 09:26:55 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[chat]]></category>
		<category><![CDATA[chat application]]></category>
		<category><![CDATA[event-machine]]></category>
		<category><![CDATA[eventmachine]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[socket programming]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=103</guid>
		<description><![CDATA[Never had chance to look into event machine before. And when I read about this today, I am really surprised &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/01/ruby-eventmachine-is-rocking/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=103&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Never had chance to look into event machine before. And when I read about this today, I am really surprised and also impressed.</p>
<p>﻿will you believe, the following code can do magic.</p>
<div id="_mcePaste">require &#8216;rubygems&#8217;</div>
<div id="_mcePaste">require &#8216;eventmachine&#8217;</div>
<div id="_mcePaste">module EchoServer</div>
<div id="_mcePaste">def post_init</div>
<div id="_mcePaste">$clients_list ||= {}</div>
<div id="_mcePaste">@identifier = self.object_id</div>
<div id="_mcePaste">$clients_list.merge!({@identifier =&gt; self})</div>
<div id="_mcePaste">end</div>
<div id="_mcePaste">def receive_data data</div>
<div id="_mcePaste"># broadcast this message to all clients</div>
<div id="_mcePaste">$clients_list.values.each do |client|</div>
<div id="_mcePaste">client.send_data(data)</div>
<div id="_mcePaste">end</div>
<div id="_mcePaste">end</div>
<div id="_mcePaste">def unbind</div>
<div id="_mcePaste">$clients_list.delete(@identifier)</div>
<div id="_mcePaste">end</div>
<div id="_mcePaste">end</div>
<div>EventMachine::run {</div>
<div id="_mcePaste">EventMachine::start_server &#8220;127.0.0.1&#8243;, 6789, EchoServer</div>
<div id="_mcePaste">}</div>
<div>save this code in a file and run it using ruby (ruby &lt;filename.rb&gt;)</div>
<div>now telnet from 2 terminals using command      telnet 127.0.0.1 6789</div>
<div>And now type from any terminal, and look a the magic, eventmachine will display the message in all terminals.</div>
<div>I found this when I was reading <a title="nodejs" href="http://nodejs.org/">nodejs</a> to build better chat application <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/103/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=103&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/01/ruby-eventmachine-is-rocking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
		<item>
		<title>change active record default error messages</title>
		<link>http://shivsingh.wordpress.com/2010/03/01/change-active-record-default-error-messages/</link>
		<comments>http://shivsingh.wordpress.com/2010/03/01/change-active-record-default-error-messages/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 09:24:47 +0000</pubDate>
		<dc:creator>shivsingh</dc:creator>
				<category><![CDATA[Ruby & Rails]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[active record error message]]></category>
		<category><![CDATA[ActiveRecord::Errors.default_error_messages]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error message]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://shivsingh.wordpress.com/?p=111</guid>
		<description><![CDATA[Do you ever thought of changing default error messages of Rails. It is too easy. Rails have all its default &#8230;<p><a href="http://shivsingh.wordpress.com/2010/03/01/change-active-record-default-error-messages/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=111&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Do you ever thought of changing default error messages of Rails.</p>
<p>It is too easy. Rails have all its default error messages in <strong><tt>ActiveRecord::Errors.default_error_messages </tt></strong><tt>hash. Now just go ahead and change any message you want as you like <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </tt><strong><tt><br />
</tt></strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shivsingh.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shivsingh.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shivsingh.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shivsingh.wordpress.com&amp;blog=3867422&amp;post=111&amp;subd=shivsingh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shivsingh.wordpress.com/2010/03/01/change-active-record-default-error-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/309b7a8041b8fbee5e200157cd7e9b33?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">shivsingh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
