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

<channel>
	<title>Lazycoder &#187; Web</title>
	<atom:link href="http://www.lazycoder.com/weblog/category/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lazycoder.com/weblog</link>
	<description></description>
	<lastBuildDate>Wed, 30 Jun 2010 20:09:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Quick jQuery hack to fix position:fixed toolbars in iPhone/iPad/iPod Touch</title>
		<link>http://www.lazycoder.com/weblog/2010/05/27/quick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch/</link>
		<comments>http://www.lazycoder.com/weblog/2010/05/27/quick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch/#comments</comments>
		<pubDate>Thu, 27 May 2010 18:29:43 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[iPod]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1357</guid>
		<description><![CDATA[This is just a quick fix if your postion:fixed elements end up in weird places when your site is viewed on the iPhone, iPad, or iPod Touch. Say you have a div with an id of "#footer" that you want to stay at the bottom of the page. If you set it's position to "fixed" [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick fix if your postion:fixed elements end up in weird places when your site is viewed on the iPhone, iPad, or iPod Touch.</p>
<p> Say you have a div with an id of "#footer" that you want to stay at the bottom of the page. If you set it's position to "fixed" and set the bottom to "0px". When viewed on an iPad, iPhone or iPod Touch, the footer <a href="http://blog.mspace.fm/2009/10/01/iphone-mobile-safari-css-position-fixed/">may end up in the middle of your content</a> if you have a long page. </p>
<div class="igBar"><span id="lcode-2"><a href="#" onclick="javascript:showCodeTxt('code-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-2">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit </span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">if<span style="color:#006600; font-weight:bold;">&#40;</span>navigator.<span style="">platform</span> == <span style="color:#CC0000;">'iPad'</span> || navigator.<span style="">platform</span> == <span style="color:#CC0000;">'iPhone'</span> || navigator.<span style="">platform</span> == <span style="color:#CC0000;">'iPod'</span><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;$<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"#footer"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"position"</span>, <span style="color:#CC0000;">"static"</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;notes=This%20is%20just%20a%20quick%20fix%20if%20your%20postion%3Afixed%20elements%20end%20up%20in%20weird%20places%20when%20your%20site%20is%20viewed%20on%20the%20iPhone%2C%20iPad%2C%20or%20iPod%20Touch.%0D%0A%0D%0A%20Say%20you%20have%20a%20div%20with%20an%20id%20of%20%22%23footer%22%20that%20you%20want%20to%20stay%20at%20the%20bottom%20of%20the%20page.%20If%20you%20set%20it%27" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;bodytext=This%20is%20just%20a%20quick%20fix%20if%20your%20postion%3Afixed%20elements%20end%20up%20in%20weird%20places%20when%20your%20site%20is%20viewed%20on%20the%20iPhone%2C%20iPad%2C%20or%20iPod%20Touch.%0D%0A%0D%0A%20Say%20you%20have%20a%20div%20with%20an%20id%20of%20%22%23footer%22%20that%20you%20want%20to%20stay%20at%20the%20bottom%20of%20the%20page.%20If%20you%20set%20it%27" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;source=Lazycoder+&amp;summary=This%20is%20just%20a%20quick%20fix%20if%20your%20postion%3Afixed%20elements%20end%20up%20in%20weird%20places%20when%20your%20site%20is%20viewed%20on%20the%20iPhone%2C%20iPad%2C%20or%20iPod%20Touch.%0D%0A%0D%0A%20Say%20you%20have%20a%20div%20with%20an%20id%20of%20%22%23footer%22%20that%20you%20want%20to%20stay%20at%20the%20bottom%20of%20the%20page.%20If%20you%20set%20it%27" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;t=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;t=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch" title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;title=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;selection=This%20is%20just%20a%20quick%20fix%20if%20your%20postion%3Afixed%20elements%20end%20up%20in%20weird%20places%20when%20your%20site%20is%20viewed%20on%20the%20iPhone%2C%20iPad%2C%20or%20iPod%20Touch.%0D%0A%0D%0A%20Say%20you%20have%20a%20div%20with%20an%20id%20of%20%22%23footer%22%20that%20you%20want%20to%20stay%20at%20the%20bottom%20of%20the%20page.%20If%20you%20set%20it%27" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F&amp;t=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch&amp;s=This%20is%20just%20a%20quick%20fix%20if%20your%20postion%3Afixed%20elements%20end%20up%20in%20weird%20places%20when%20your%20site%20is%20viewed%20on%20the%20iPhone%2C%20iPad%2C%20or%20iPod%20Touch.%0D%0A%0D%0A%20Say%20you%20have%20a%20div%20with%20an%20id%20of%20%22%23footer%22%20that%20you%20want%20to%20stay%20at%20the%20bottom%20of%20the%20page.%20If%20you%20set%20it%27" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Quick%20jQuery%20hack%20to%20fix%20position%3Afixed%20toolbars%20in%20iPhone%2FiPad%2FiPod%20Touch%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2010%2F05%2F27%2Fquick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2010/05/27/quick-jquery-hack-to-fix-positionfixed-toolbars-in-iphoneipadipod-touch/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Benchmarking a simple DOM based cloning template</title>
		<link>http://www.lazycoder.com/weblog/2009/12/10/benchmarking-a-simple-dom-based-cloning-template/</link>
		<comments>http://www.lazycoder.com/weblog/2009/12/10/benchmarking-a-simple-dom-based-cloning-template/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 20:28:02 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1232</guid>
		<description><![CDATA[Sara Chipps recently posted a simple DOM based clone template method she uses in one of her apps. "Easy HTML Templating with JQuery" My template looks like this: PLAIN TEXT CODE: &#60;script id="ItemTemplate" type="text/html" &#160; &#160; &#160; &#160; &#60;li class="item" value="&#124;rowNumber&#124;"&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#60;input type=”text” id=”input&#124;rowNumber&#124;” /&#62; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Sara Chipps recently posted a simple <acronym title='Document Object Model'><span class='caps'>DOM</span></acronym> based clone template method she uses in one of her apps. "<a href="http://girldeveloper.com/waxing-dev/easy-html-templating-with-jquery/">Easy <acronym title='HyperText Markup Language'><span class='caps'>HTML</span></acronym> Templating with JQuery</a>"</p>
<blockquote><p>
My template looks like this:</p>
<div class="igBar"><span id="lcode-9"><a href="#" onclick="javascript:showCodeTxt('code-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-9">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script id=<span style="color:#CC0000;">"ItemTemplate"</span> type=<span style="color:#CC0000;">"text/html"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;li class=<span style="color:#CC0000;">"item"</span> value=<span style="color:#CC0000;">"|rowNumber|"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=”text” id=”input|rowNumber|” /&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/li&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p> <br />
Now within my code I need to put a place holder where I want my <acronym title='HyperText Markup Language'><span class='caps'>HTML</span></acronym> to go.  I have my unordered list called url_list.</p>
<div class="igBar"><span id="lcode-10"><a href="#" onclick="javascript:showCodeTxt('code-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-10">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;ul id=<span style="color:#CC0000;">"url_list"</span>&gt;&lt;/ul&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, you see that most of my items look like this “|rowNumber|” I have a variable in my code called nextUniqueItemID (I believe in extremely descriptive variable names). Here is my “addItem” function.</p>
<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showCodeTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> addItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> list = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#url_list'</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = list.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; list.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#ItemTemplate’)</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #3366CC;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .html().replace(/<span style="color: #000099; font-weight: bold;">\|</span>rowNumber<span style="color: #000099; font-weight: bold;">\|</span>/gi, nextUniqueItemID++))</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #3366CC;">&nbsp; &nbsp; } </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
</p></blockquote>
<p>The use of global variables aside (cough,cough),I looked at it and, having used something like this myself, thought that it would work find for data sets containing a very small number of items. The problem is these kinds of clone based templates are VERY slow compared to the templating engines that are available for various JavaScript libraries.</p>
<p>I happened to read a post by Brian Landau called "<a href="http://www.viget.com/extend/benchmarking-javascript-templating-libraries/">Benchmarking Javascript Templating Libraries</a>" this morning and wondered just HOW MUCH slower is the naive template method than a good template library?</p>
<p>I grabbed the benchmarking code and modified it to run the new clone based template method.</p>
<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showCodeTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-12">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> nextUniqueItemID = <span style="color: #CC0000;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> addItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> list = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#url_list'</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; items = list.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; list.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#ItemTemplate'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\|rowNumber\|/gi</span>, nextUniqueItemID++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> output = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#output'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$.<span style="color: #006600;">benchmarks</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; $.<span style="color: #006600;">benchmarks</span>.<span style="color: #006600;">test_simple</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; addItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; $.<span style="color: #006600;">benchmarks</span>.<span style="color: #006600;">loop_test</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;color:#800000;">0</span>; i &lt;<span style="color: #CC0000;color:#800000;">5</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// use these lines to run the benchmark tests in your browsers JS console</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// $.benchmark(1000, '#simple_test', $.benchmarks.test_simple);</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// $.benchmark(1000, '#loop_test', $.benchmarks.loop_test);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Since the template Sara provided contains an input tag you get a different benchmark if you run the simple_test and the loop_test separately after refreshing your browser. You can run the tests for yourself<a href="http://lazycoder.com/code_examples/templatebenchmarks/"> here</a>, the loop test *may* cause your browser to give you a "script is running slow" message, hit continue as the loop <strong>will</strong> eventually end. You may also get different numbers if you run the tests in <acronym title='Internet Explorer'><span class='caps'>IE</span></acronym>, Chrome, and Safari.</p>
<p>results: using FF 3.5.5<br />
Simple Test: 1.71s<br />
Loop test: 31.534s</p>
<p>When you consider that the slowest loop test using a template library was just around 4.5s, you get a better idea of just how slow this method is when you have an input in your template. </p>
<p>So that's fine, but it's known that dynamically adding text inputs is slow in just about every browser and the original tests don't use inputs at all, just divs. So let's modify the template and see what the results are.</p>
<div class="igBar"><span id="lcode-13"><a href="#" onclick="javascript:showCodeTxt('code-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-13">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script id=<span style="color:#CC0000;">"ItemTemplate"</span> type=<span style="color:#CC0000;">"text/html"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;div class=<span style="color:#CC0000;">"test"</span>&gt;&lt;h2&gt;This is a test of |name|&lt;/h2&gt;&lt;p&gt;The homepage is &lt;a href=<span style="color:#CC0000;">"|url|"</span>&gt;|url|&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The sources is: |source|&lt;/p&gt;&lt;/div&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I modified the addItem function to account for the new data. n.b. The data I'm using is static, if you wanted to use a data source you would just modify this method to take in your data parameters.</p>
<div class="igBar"><span id="ljavascript-14"><a href="#" onclick="javascript:showCodeTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-14">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> addItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> list = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#url_list'</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; items = list.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; list.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#ItemTemplate'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\|name\|/gi</span>, <span style="color: #3366CC;">"Clone template method"</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\|source\|/gi</span>, <span style="color: #3366CC;">"http://girldeveloper.com/waxing-dev/easy-html-templating-with-jquery/"</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/\|url\|/gi</span>, <span style="color: #3366CC;">"http://girldeveloper.com/waxing-dev/easy-html-templating-with-jquery/"</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
results using FF 3.5.5 - refresh between each test<br />
simple test: 1.285s<br />
loop test: 3.771</p>
<p>results using ff 3.5.5 with no refresh between tests<br />
simple test: 1.434<br />
loop test: 4.227</p>
<p>So that's looking a little bit better. Not too much slower than the template libraries.</p>
<p>So what do the template libraries give you? Well the replace method works find provides your data is escaped properly. But say instead of a url in the "source" replacement, you use a file path like "file:\\foodrive\source.txt". Well it still gets replaced, but the text looks like this "file:\foodrivesource.txt". So in addition to the replacement, you have to make sure your data is properly escaped. A lot of template libraries will do this for you. Also notice that the addItem method has to do a <acronym title='Document Object Model'><span class='caps'>DOM</span></acronym> lookup on every iteration of the loop to get the template. If you have a large <acronym title='Document Object Model'><span class='caps'>DOM</span></acronym>, this could impact the performance.</p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;notes=Sara%20Chipps%20recently%20posted%20a%20simple%20DOM%20based%20clone%20template%20method%20she%20uses%20in%20one%20of%20her%20apps.%20%22Easy%20HTML%20Templating%20with%20JQuery%22%0D%0A%0D%0AMy%20template%20looks%20like%20this%3A%0D%0A%5Bcode%5D%0D%0A%20%20" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;bodytext=Sara%20Chipps%20recently%20posted%20a%20simple%20DOM%20based%20clone%20template%20method%20she%20uses%20in%20one%20of%20her%20apps.%20%22Easy%20HTML%20Templating%20with%20JQuery%22%0D%0A%0D%0AMy%20template%20looks%20like%20this%3A%0D%0A%5Bcode%5D%0D%0A%20%20" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;source=Lazycoder+&amp;summary=Sara%20Chipps%20recently%20posted%20a%20simple%20DOM%20based%20clone%20template%20method%20she%20uses%20in%20one%20of%20her%20apps.%20%22Easy%20HTML%20Templating%20with%20JQuery%22%0D%0A%0D%0AMy%20template%20looks%20like%20this%3A%0D%0A%5Bcode%5D%0D%0A%20%20" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;t=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;t=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template" title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;title=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;selection=Sara%20Chipps%20recently%20posted%20a%20simple%20DOM%20based%20clone%20template%20method%20she%20uses%20in%20one%20of%20her%20apps.%20%22Easy%20HTML%20Templating%20with%20JQuery%22%0D%0A%0D%0AMy%20template%20looks%20like%20this%3A%0D%0A%5Bcode%5D%0D%0A%20%20" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F&amp;t=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template&amp;s=Sara%20Chipps%20recently%20posted%20a%20simple%20DOM%20based%20clone%20template%20method%20she%20uses%20in%20one%20of%20her%20apps.%20%22Easy%20HTML%20Templating%20with%20JQuery%22%0D%0A%0D%0AMy%20template%20looks%20like%20this%3A%0D%0A%5Bcode%5D%0D%0A%20%20" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Benchmarking%20a%20simple%20DOM%20based%20cloning%20template%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F12%2F10%2Fbenchmarking-a-simple-dom-based-cloning-template%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2009/12/10/benchmarking-a-simple-dom-based-cloning-template/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>A new JavaScript CDN from Microsoft</title>
		<link>http://www.lazycoder.com/weblog/2009/09/29/a-new-javascript-cdn-from-microsoft/</link>
		<comments>http://www.lazycoder.com/weblog/2009/09/29/a-new-javascript-cdn-from-microsoft/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 23:36:55 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1173</guid>
		<description><![CDATA[Microsoft announced a new Content Delivery Network for their ASP.NET AJAX JavaScript libraries and jQuery. This means that instead of hosting those libraries on your server, you can just link to the versions on Microsofts server. I made a simple page that takes a querystring parameter (q=) and uses the ASP.NET AJAX dynamic templates to [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft announced a new Content Delivery Network for their ASP.NET <acronym title='Asynchronous Javascript and XML'><span class='caps'>AJAX</span></acronym> JavaScript libraries and jQuery. This means that instead of hosting those libraries on your server, you can just link to the versions on Microsofts server. I made a <a href="http://lazycoder.com/letmebingthatforyou/?q=jQuery">simple page</a> that takes a querystring parameter (q=) and uses the ASP.NET <acronym title='Asynchronous Javascript and XML'><span class='caps'>AJAX</span></acronym> dynamic templates to bind search results from a call to the Bing <acronym title='Application Interface'><span class='caps'>API</span></acronym>.</p>
<p>The money lines in the source are the following:</p>
<div class="igBar"><span id="lcode-18"><a href="#" onclick="javascript:showCodeTxt('code-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-18">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">src=<span style="color:#CC0000;">"http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjax.debug.js"</span>&gt;&lt;/script&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color:#CC0000;">"text/javascript"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">src=<span style="color:#CC0000;">"http://ajax.microsoft.com/ajax/beta/0909/MicrosoftAjaxTemplates.js"</span>&gt;&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
These two lines tell the browser to load the MS <acronym title='Asynchronous Javascript and XML'><span class='caps'>AJAX</span></acronym> scripts from the CDN. There are some <a href="http://idunno.org/archive/2009/09/16/quick-thoughts-on-the-microsoft-ajax-cdn.aspx">security concerns</a> around the fact that the files are served from the microsoft.com domain. Both Google and Yahoo serve there files from a separate, non-cookied domain (googleapis.com and yahooapis.com respectively). Hopefully, these fears will be unfounded.</p>
<p>On a side note, it is surprisingly easy to use the Bing <acronym title='Application Interface'><span class='caps'>API</span></acronym> if you are familiar with script tag injection. The easiest way is to put an empty script tag in your page.</p>
<div class="igBar"><span id="lcode-19"><a href="#" onclick="javascript:showCodeTxt('code-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-19">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script id=<span style="color:#CC0000;">"jsonResults"</span> type=<span style="color:#CC0000;">"text/javascript"</span>&gt;&lt;/script&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And then just create your Bing <acronym title='Uniform Resource Locator'><span class='caps'>URL</span></acronym> and set the script elements src attribute to the <acronym title='Uniform Resource Locator'><span class='caps'>URL</span></acronym> you created.</p>
<div class="igBar"><span id="lcode-20"><a href="#" onclick="javascript:showCodeTxt('code-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-20">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function MakeSearchRequest<span style="color:#006600; font-weight:bold;">&#40;</span>searchPhrase<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var req = <span style="color:#CC0000;">"http://api.bing.net/json.aspx?"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ <span style="color:#CC0000;">"AppId="</span> + YOUR <acronym title='Application Interface'><span class='caps'>API</span></acronym> KEY GOES HERE</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ <span style="color:#CC0000;">"&amp;Query="</span> + searchPhrase</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ <span style="color:#CC0000;">"&amp;Sources=Web"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ <span style="color:#CC0000;">"&amp;JsonType=callback"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ <span style="color:#CC0000;">"&amp;JsonCallback=BuildResults"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"jsonResults"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">src</span> = req;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;notes=Microsoft%20announced%20a%20new%20Content%20Delivery%20Network%20for%20their%20ASP.NET%20AJAX%20JavaScript%20libraries%20and%20jQuery.%20This%20means%20that%20instead%20of%20hosting%20those%20libraries%20on%20your%20server%2C%20you%20can%20just%20link%20to%20the%20versions%20on%20Microsofts%20server.%20I%20made%20a%20simple%20page" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;bodytext=Microsoft%20announced%20a%20new%20Content%20Delivery%20Network%20for%20their%20ASP.NET%20AJAX%20JavaScript%20libraries%20and%20jQuery.%20This%20means%20that%20instead%20of%20hosting%20those%20libraries%20on%20your%20server%2C%20you%20can%20just%20link%20to%20the%20versions%20on%20Microsofts%20server.%20I%20made%20a%20simple%20page" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;source=Lazycoder+&amp;summary=Microsoft%20announced%20a%20new%20Content%20Delivery%20Network%20for%20their%20ASP.NET%20AJAX%20JavaScript%20libraries%20and%20jQuery.%20This%20means%20that%20instead%20of%20hosting%20those%20libraries%20on%20your%20server%2C%20you%20can%20just%20link%20to%20the%20versions%20on%20Microsofts%20server.%20I%20made%20a%20simple%20page" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;t=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;t=A%20new%20JavaScript%20CDN%20from%20Microsoft" title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;title=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;selection=Microsoft%20announced%20a%20new%20Content%20Delivery%20Network%20for%20their%20ASP.NET%20AJAX%20JavaScript%20libraries%20and%20jQuery.%20This%20means%20that%20instead%20of%20hosting%20those%20libraries%20on%20your%20server%2C%20you%20can%20just%20link%20to%20the%20versions%20on%20Microsofts%20server.%20I%20made%20a%20simple%20page" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F&amp;t=A%20new%20JavaScript%20CDN%20from%20Microsoft&amp;s=Microsoft%20announced%20a%20new%20Content%20Delivery%20Network%20for%20their%20ASP.NET%20AJAX%20JavaScript%20libraries%20and%20jQuery.%20This%20means%20that%20instead%20of%20hosting%20those%20libraries%20on%20your%20server%2C%20you%20can%20just%20link%20to%20the%20versions%20on%20Microsofts%20server.%20I%20made%20a%20simple%20page" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=A%20new%20JavaScript%20CDN%20from%20Microsoft%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F09%2F29%2Fa-new-javascript-cdn-from-microsoft%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2009/09/29/a-new-javascript-cdn-from-microsoft/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC Tip &#8211; Return specific views for specific errors.</title>
		<link>http://www.lazycoder.com/weblog/2009/03/18/aspnet-mvc-tip-return-specific-views-for-specific-errors/</link>
		<comments>http://www.lazycoder.com/weblog/2009/03/18/aspnet-mvc-tip-return-specific-views-for-specific-errors/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 15:30:28 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1114</guid>
		<description><![CDATA[Earlier I had said to keep your controllers as thin as possible, that doesn't mean that they should necessarily just be two, or one, lines of code. Take an instance where you are retrieving items from a web service in your controller. Let's say that you get a 404 error and your service will throw [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier I had said to keep your controllers as thin as possible, that doesn't mean that they should necessarily just be two, or one, lines of code. </p>
<p>Take an instance where you are retrieving items from a web service in your controller. Let's say that you get a 404 error and your service will throw an exception telling you that the service can't be found.  Take a look at this controller action</p>
<div class="igBar"><span id="lcode-23"><a href="#" onclick="javascript:showCodeTxt('code-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-23">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public ViewResult Item<span style="color:#006600; font-weight:bold;">&#40;</span>string itemID<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Item item = MyWebServiceWrapper.<span style="">GetItem</span><span style="color:#006600; font-weight:bold;">&#40;</span>itemID<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return View<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"Item"</span>, item<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>As it stands this action is pretty thin, but if an exception is thrown the user will be presented with either whatever custom error page you have defined in your web.config or the dreaded Yellow Screen of Death.(YSOD). You can wrap the call in a try-catch block and catch the exception. But rather than creating a generic errors page that will display any raw exception message, it's a much better idea to return an error specific view to your user with a friendly error message.</p>
<div class="igBar"><span id="lcode-24"><a href="#" onclick="javascript:showCodeTxt('code-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-24">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public ViewResult Item<span style="color:#006600; font-weight:bold;">&#40;</span>string itemID<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; try</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Item item = MyWebServiceWrapper.<span style="">GetItem</span><span style="color:#006600; font-weight:bold;">&#40;</span>itemID<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; return View<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"Item"</span>, item<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; catch<span style="color:#006600; font-weight:bold;">&#40;</span>ServiceCannotBeReachedException<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Item item = new Item<span style="color:#006600; font-weight:bold;">&#40;</span>itemID<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; return View<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"CannotRetrieveItem"</span>, item<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//The CannotReachService view has a line that looks like this</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// &quot;The item &lt;%= Model.itemID %&gt; can not be retrieved at this time. Please try again later&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// obviously this can be globalized/localized as needed. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This leads to a much "dumber" view and is a much better idea than modifying the "Item" view to display errors if something isn't right with the model or passing in special error parameters to the view. Let the view deal with the data it is meant to display and nothing else.</p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;notes=Earlier%20I%20had%20said%20to%20keep%20your%20controllers%20as%20thin%20as%20possible%2C%20that%20doesn%27t%20mean%20that%20they%20should%20necessarily%20just%20be%20two%2C%20or%20one%2C%20lines%20of%20code.%20%0A%0ATake%20an%20instance%20where%20you%20are%20retrieving%20items%20from%20a%20web%20service%20in%20your%20controller.%20Let%27s%20say%20tha" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;bodytext=Earlier%20I%20had%20said%20to%20keep%20your%20controllers%20as%20thin%20as%20possible%2C%20that%20doesn%27t%20mean%20that%20they%20should%20necessarily%20just%20be%20two%2C%20or%20one%2C%20lines%20of%20code.%20%0A%0ATake%20an%20instance%20where%20you%20are%20retrieving%20items%20from%20a%20web%20service%20in%20your%20controller.%20Let%27s%20say%20tha" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;source=Lazycoder+&amp;summary=Earlier%20I%20had%20said%20to%20keep%20your%20controllers%20as%20thin%20as%20possible%2C%20that%20doesn%27t%20mean%20that%20they%20should%20necessarily%20just%20be%20two%2C%20or%20one%2C%20lines%20of%20code.%20%0A%0ATake%20an%20instance%20where%20you%20are%20retrieving%20items%20from%20a%20web%20service%20in%20your%20controller.%20Let%27s%20say%20tha" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;t=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;t=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors." title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;title=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;selection=Earlier%20I%20had%20said%20to%20keep%20your%20controllers%20as%20thin%20as%20possible%2C%20that%20doesn%27t%20mean%20that%20they%20should%20necessarily%20just%20be%20two%2C%20or%20one%2C%20lines%20of%20code.%20%0A%0ATake%20an%20instance%20where%20you%20are%20retrieving%20items%20from%20a%20web%20service%20in%20your%20controller.%20Let%27s%20say%20tha" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F&amp;t=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.&amp;s=Earlier%20I%20had%20said%20to%20keep%20your%20controllers%20as%20thin%20as%20possible%2C%20that%20doesn%27t%20mean%20that%20they%20should%20necessarily%20just%20be%20two%2C%20or%20one%2C%20lines%20of%20code.%20%0A%0ATake%20an%20instance%20where%20you%20are%20retrieving%20items%20from%20a%20web%20service%20in%20your%20controller.%20Let%27s%20say%20tha" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=ASP.NET%20MVC%20Tip%20-%20Return%20specific%20views%20for%20specific%20errors.%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F18%2Faspnet-mvc-tip-return-specific-views-for-specific-errors%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2009/03/18/aspnet-mvc-tip-return-specific-views-for-specific-errors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC tip &#8211; Don&#8217;t use the Content or Scripts directories for view specific files</title>
		<link>http://www.lazycoder.com/weblog/2009/03/17/aspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files/</link>
		<comments>http://www.lazycoder.com/weblog/2009/03/17/aspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 15:39:31 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1112</guid>
		<description><![CDATA[ASP.NET MVC creates a default file structure for you when you create a new project. It includes a Scripts directory, which contains the MS AJAX and jQuery .js files, and a content directory, which contains a Master page and CSS files. I find this to be extremely cumbersome and it forces me to jump around [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET MVC creates a default file structure for you when you create a new project. It includes a Scripts directory, which contains the MS <acronym title='Asynchronous Javascript and XML'><span class='caps'>AJAX</span></acronym> and jQuery .js files, and a content directory, which contains a Master page and <acronym title='Cascading Style Sheets'><span class='caps'>CSS</span></acronym> files. I find this to be extremely cumbersome and it forces me to jump around a lot. Storing shared <acronym title='Cascading Style Sheets'><span class='caps'>CSS</span></acronym> and JavaScript files in those locations is fine in my opinion. But if you are using View specific <acronym title='Cascading Style Sheets'><span class='caps'>CSS</span></acronym> or JavaScript files, you should put them alongside your view page in the Views directory. This allows you to quickly find the file you want to work on.</p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;notes=ASP.NET%20MVC%20creates%20a%20default%20file%20structure%20for%20you%20when%20you%20create%20a%20new%20project.%20It%20includes%20a%20Scripts%20directory%2C%20which%20contains%20the%20MS%20AJAX%20and%20jQuery%20.js%20files%2C%20and%20a%20content%20directory%2C%20which%20contains%20a%20Master%20page%20and%20CSS%20files.%20I%20find%20this%20to%20" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;bodytext=ASP.NET%20MVC%20creates%20a%20default%20file%20structure%20for%20you%20when%20you%20create%20a%20new%20project.%20It%20includes%20a%20Scripts%20directory%2C%20which%20contains%20the%20MS%20AJAX%20and%20jQuery%20.js%20files%2C%20and%20a%20content%20directory%2C%20which%20contains%20a%20Master%20page%20and%20CSS%20files.%20I%20find%20this%20to%20" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;source=Lazycoder+&amp;summary=ASP.NET%20MVC%20creates%20a%20default%20file%20structure%20for%20you%20when%20you%20create%20a%20new%20project.%20It%20includes%20a%20Scripts%20directory%2C%20which%20contains%20the%20MS%20AJAX%20and%20jQuery%20.js%20files%2C%20and%20a%20content%20directory%2C%20which%20contains%20a%20Master%20page%20and%20CSS%20files.%20I%20find%20this%20to%20" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;t=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;t=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files" title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;title=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;selection=ASP.NET%20MVC%20creates%20a%20default%20file%20structure%20for%20you%20when%20you%20create%20a%20new%20project.%20It%20includes%20a%20Scripts%20directory%2C%20which%20contains%20the%20MS%20AJAX%20and%20jQuery%20.js%20files%2C%20and%20a%20content%20directory%2C%20which%20contains%20a%20Master%20page%20and%20CSS%20files.%20I%20find%20this%20to%20" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F&amp;t=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files&amp;s=ASP.NET%20MVC%20creates%20a%20default%20file%20structure%20for%20you%20when%20you%20create%20a%20new%20project.%20It%20includes%20a%20Scripts%20directory%2C%20which%20contains%20the%20MS%20AJAX%20and%20jQuery%20.js%20files%2C%20and%20a%20content%20directory%2C%20which%20contains%20a%20Master%20page%20and%20CSS%20files.%20I%20find%20this%20to%20" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=ASP.NET%20MVC%20tip%20-%20Don%27t%20use%20the%20Content%20or%20Scripts%20directories%20for%20view%20specific%20files%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F17%2Faspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2009/03/17/aspnet-mvc-tip-dont-use-the-content-or-scripts-directories-for-view-specific-files/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC TIP &#8211; Keep your controllers and actions thin</title>
		<link>http://www.lazycoder.com/weblog/2009/03/16/aspnet-mvc-tip-keep-your-controllers-and-actions-thin/</link>
		<comments>http://www.lazycoder.com/weblog/2009/03/16/aspnet-mvc-tip-keep-your-controllers-and-actions-thin/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 15:16:03 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.lazycoder.com/weblog/?p=1110</guid>
		<description><![CDATA[When writing you controller actions, keep them short and sweet. Don't add a lot of actions and keep a close eye on the length of each action. Whenever possible, I try to make my controller actions look something like this. PLAIN TEXT CODE: public ActionResult DoSomething&#40;&#41; &#123; &#160; &#160; MyModel model = MyService.GetModel&#40;&#41;; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>When writing you controller actions, keep them short and sweet. Don't add a lot of actions and keep a close eye on the length of each action.<br />
Whenever possible, I try to make my controller actions look something like this.</p>
<div class="igBar"><span id="lcode-26"><a href="#" onclick="javascript:showCodeTxt('code-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-26">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public ActionResult DoSomething<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; MyModel model = MyService.<span style="">GetModel</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return View<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"MyView"</span>,model<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//or</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public ActionResult DoSomethingWithAKey<span style="color:#006600; font-weight:bold;">&#40;</span>int myKey<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; MyModel model = MyService.<span style="">GetModel</span><span style="color:#006600; font-weight:bold;">&#40;</span>myKey<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return View<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"MyView"</span>,model<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p> <em>(1)</em></p>
<p>There are two reasons to do this:</p>
<p>1) It makes your controllers less complicated and easier to maintain. - You don't want to have to do a lot of debugging in your controllers. Chances are, if you have a lot of complicated logic in your controller actions, you are probably mixing concerns. Make sure that your controllers are only responsible for getting data from your models and figuring out what view to send to the client.</p>
<p>2) It makes testing easier. If all you are doing is getting a model and passing that model to a view, what do you really need to test? Maybe that the correct view is being returned.</p>
<p><em>(1)</em> Of course I use more descriptive names than "myKey". <img src='http://www.lazycoder.com/weblog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>



Share and Enjoy:


	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;notes=When%20writing%20you%20controller%20actions%2C%20keep%20them%20short%20and%20sweet.%20Don%27t%20add%20a%20lot%20of%20actions%20and%20keep%20a%20close%20eye%20on%20the%20length%20of%20each%20action.%20%0AWhenever%20possible%2C%20I%20try%20to%20make%20my%20controller%20actions%20look%20something%20like%20this.%0A%5Bcode%5D%0Apublic%20ActionResult" title="del.icio.us"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="DotNetKicks"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="DZone"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="Reddit"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;bodytext=When%20writing%20you%20controller%20actions%2C%20keep%20them%20short%20and%20sweet.%20Don%27t%20add%20a%20lot%20of%20actions%20and%20keep%20a%20close%20eye%20on%20the%20length%20of%20each%20action.%20%0AWhenever%20possible%2C%20I%20try%20to%20make%20my%20controller%20actions%20look%20something%20like%20this.%0A%5Bcode%5D%0Apublic%20ActionResult" title="Digg"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="StumbleUpon"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;source=Lazycoder+&amp;summary=When%20writing%20you%20controller%20actions%2C%20keep%20them%20short%20and%20sweet.%20Don%27t%20add%20a%20lot%20of%20actions%20and%20keep%20a%20close%20eye%20on%20the%20length%20of%20each%20action.%20%0AWhenever%20possible%2C%20I%20try%20to%20make%20my%20controller%20actions%20look%20something%20like%20this.%0A%5Bcode%5D%0Apublic%20ActionResult" title="LinkedIn"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;t=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="Facebook"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;link=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F" title="FriendFeed"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;t=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin" title="HackerNews"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.netvibes.com/share?title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;url=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F" title="Netvibes"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://posterous.com/share?linkto=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;title=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;selection=When%20writing%20you%20controller%20actions%2C%20keep%20them%20short%20and%20sweet.%20Don%27t%20add%20a%20lot%20of%20actions%20and%20keep%20a%20close%20eye%20on%20the%20length%20of%20each%20action.%20%0AWhenever%20possible%2C%20I%20try%20to%20make%20my%20controller%20actions%20look%20something%20like%20this.%0A%5Bcode%5D%0Apublic%20ActionResult" title="Posterous"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/posterous.png" title="Posterous" alt="Posterous" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F&amp;t=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin&amp;s=When%20writing%20you%20controller%20actions%2C%20keep%20them%20short%20and%20sweet.%20Don%27t%20add%20a%20lot%20of%20actions%20and%20keep%20a%20close%20eye%20on%20the%20length%20of%20each%20action.%20%0AWhenever%20possible%2C%20I%20try%20to%20make%20my%20controller%20actions%20look%20something%20like%20this.%0A%5Bcode%5D%0Apublic%20ActionResult" title="Tumblr"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=ASP.NET%20MVC%20TIP%20-%20Keep%20your%20controllers%20and%20actions%20thin%20-%20http%3A%2F%2Fwww.lazycoder.com%2Fweblog%2F2009%2F03%2F16%2Faspnet-mvc-tip-keep-your-controllers-and-actions-thin%2F" title="Twitter"><img src="http://www.lazycoder.com/weblog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.lazycoder.com/weblog/2009/03/16/aspnet-mvc-tip-keep-your-controllers-and-actions-thin/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
