<?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>sides of march &#187; MonoRail</title>
	<atom:link href="http://www.sidesofmarch.com/index.php/archive/tag/monorail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sidesofmarch.com</link>
	<description>Thoughts on life, liberty, and information technology</description>
	<lastBuildDate>Mon, 16 Jan 2012 02:43:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using MonoRail ViewComponents with the Spark view engine</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/02/02/using-monorail-viewcomponents-with-the-spark-view-engine/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/02/02/using-monorail-viewcomponents-with-the-spark-view-engine/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 05:26:13 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[Spark]]></category>
		<category><![CDATA[ViewComponent]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=336</guid>
		<description><![CDATA[<p>Although I was able to find some documentation and samples (mostly through the unit tests) of how to use a <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/viewcomponents.html">ViewComponent</a> with the <a href="http://dev.dejardin.org/">Spark view engine</a>, details were sketchy, so I&#8217;ll share some quick tips to those who are scratching their heads as I was.</p>
<p><strong>Using a block ViewComponent</strong></p>
<p>Support for <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/viewcomponents.html#blockandsections">block view components</a> is pretty evident in Spark, as shown in the following example, which illustrates the use of the AuthenticatedContent view component.</p>


&#60;authenticatedcontent&#62;
	&#60;logged&#62;
		This content is shown when the user is logged in.
	&#60;/logged&#62;
	&#60;notlogged&#62;
		This content is shown when the user is NOT logged in.
	&#60;/notlogged&#62;
&#60;/authenticatedcontent&#62;

<p><strong>Using the CaptureFor ViewComponent</strong></p>
<p>The <a href="http://www.castleproject.org/monorail/documentation/trunk/viewcomponents/capturefor.html">CaptureFor component</a> is one useful way to allow a sub-view to &#8220;inject&#8221; data into a layout. I use this all the time to allow a rescue page to change the page title when an error occurs.</p>
<p>On your layout page, you inject the page title as you normally would (so a controller can inject it via the property bag).</p>


&#60;title&#62;ComputerSims Baseball: $!{ViewData[&#34;PageTitle&#34;]}&#60;/title&#62;

<p>In the view, the <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/02/02/using-monorail-viewcomponents-with-the-spark-view-engine/">Using MonoRail ViewComponents with the Spark view engine</a></span>]]></description>
			<content:encoded><![CDATA[<p>Although I was able to find some documentation and samples (mostly through the unit tests) of how to use a <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Fviewcomponents.html&sref=rss">ViewComponent</a> with the <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fdev.dejardin.org%2F&sref=rss">Spark view engine</a>, details were sketchy, so I&#8217;ll share some quick tips to those who are scratching their heads as I was.</p>
<p><strong>Using a block ViewComponent</strong></p>
<p>Support for <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Fviewcomponents.html%23blockandsections&sref=rss">block view components</a> is pretty evident in Spark, as shown in the following example, which illustrates the use of the AuthenticatedContent view component.</p>
<pre class="brush: html; ">

&lt;authenticatedcontent&gt;
	&lt;logged&gt;
		This content is shown when the user is logged in.
	&lt;/logged&gt;
	&lt;notlogged&gt;
		This content is shown when the user is NOT logged in.
	&lt;/notlogged&gt;
&lt;/authenticatedcontent&gt;
</pre>
<p><strong>Using the CaptureFor ViewComponent</strong></p>
<p>The <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fviewcomponents%2Fcapturefor.html&sref=rss">CaptureFor component</a> is one useful way to allow a sub-view to &#8220;inject&#8221; data into a layout. I use this all the time to allow a rescue page to change the page title when an error occurs.</p>
<p>On your layout page, you inject the page title as you normally would (so a controller can inject it via the property bag).</p>
<pre class="brush: html; ">

&lt;title&gt;ComputerSims Baseball: $!{ViewData[&quot;PageTitle&quot;]}&lt;/title&gt;
</pre>
<p>In the view, the CaptureFor component is called as an HTML tag.</p>
<pre class="brush: html; ">

&lt;capturefor id=&quot;pageTitle&quot;&gt;An unexpected error has occurred&lt;/capturefor&gt;
</pre>
<p><strong>Calling a ViewComponent with view and static data</strong></p>
<p>This is one that perplexed me. I have a custom menu component (called <code>MenuComponent</code>) which accepts a name parameter. I tried calling it like this:</p>
<pre class="brush: html; ">

&lt;menu name=&quot;mainmenu&quot; /&gt;
</pre>
<p>Unfortunately, that threw an error: &#8220;<em>The name &#8216;mainmenu&#8217; does not exist in the current context</em>&#8220;. Fortunately, Spark exceptions are easy to read (you can see the C# code it generates and thus find the error). Apparently, in the above example, &#8220;mainmenu&#8221; was expected to be a variable in the view page and was not being treated as static text. To use static text, you put single quotes inside the double quotes.</p>
<pre class="brush: html; ">

&lt;menu name=&quot;&#039;mainmenu&quot; /&gt;
</pre>
<p>Problem solved. I can&#8217;t say it&#8217;s the preferred way based on my experience thus far (I&#8217;d prefer requiring <code>${mainmenu}</code> for variable injection), but since I&#8217;m new to this, I won&#8217;t criticize much.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=336&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/02/02/using-monorail-viewcomponents-with-the-spark-view-engine/' addthis:title='Using MonoRail ViewComponents with the Spark view engine ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/02/02/using-monorail-viewcomponents-with-the-spark-view-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple layouts and views with Spark and MonoRail</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/01/29/simple-layouts-with-spark-and-monorail/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/01/29/simple-layouts-with-spark-and-monorail/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 03:08:12 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=335</guid>
		<description><![CDATA[<p>Yesterday, I wrote about the coolness of <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/">The Spark view engine (and a brief history of web programming)</a>. As I explore more, I’ll share my experiences here in hope that it shortens other people’s learning curve.</p>
<p>Today’s topic is the basics: <strong>writing a simple layout</strong>.</p>
<p>Spark offers full support for <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/layouts.html">layouts</a> and <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/views.html">views</a>. As with any other view engine, the file extension (by default, .spark) of both your layout and view files must match.</p>
<p>A simple layout looks like this:</p>


&#60;html&#62;
&#60;head&#62;
&#60;title&#62;A simple layout&#60;/title&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;h1&#62;A simple layout&#60;/h1&#62;
&#60;use content=&#34;view&#34; /&#62;
&#60;/body&#62;
&#60;/html&#62;

<p>The only thing that isn&#8217;t HTML is the &#60;use content="view" /&#62; tag. This tag denotes where in the layout (master template) to embed the content from your view. In NVelocity, we used ${ChildContent} declaration in <a href="http://www.castleproject.org/monorail/documentation/trunk/viewengines/nvelocity/index.html">NVelocity</a>, or the ${ChildOutput} declaration in <a href="http://www.castleproject.org/monorail/documentation/trunk/viewengines/brail/usersguide.html#layouts">Brail</a>. Yes, it&#8217;s that simple.</p>
<p>Often times, we want to inject some view data into our layout, such as the page title. Let&#8217;s say our controller was sending a PageTitle to the view:</p>


public void <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/29/simple-layouts-with-spark-and-monorail/">Simple layouts and views with Spark and MonoRail</a></span>]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote about the coolness of <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/">The Spark view engine (and a brief history of web programming)</a>. As I explore more, I’ll share my experiences here in hope that it shortens other people’s learning curve.</p>
<p>Today’s topic is the basics: <strong>writing a simple layout</strong>.</p>
<p>Spark offers full support for <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Flayouts.html&sref=rss">layouts</a> and <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Fviews.html&sref=rss">views</a>. As with any other view engine, the file extension (by default, <code>.spark</code>) of both your layout and view files must match.</p>
<p>A simple layout looks like this:</p>
<pre class="brush: html; ">

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;A simple layout&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;A simple layout&lt;/h1&gt;
&lt;use content=&quot;view&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The only thing that isn&#8217;t HTML is the <code>&lt;use content="view" /&gt;</code> tag. This tag denotes where in the layout (master template) to embed the content from your view. In NVelocity, we used <code>${ChildContent}</code> declaration in <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fviewengines%2Fnvelocity%2Findex.html&sref=rss">NVelocity</a>, or the <code>${ChildOutput}</code> declaration in <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fviewengines%2Fbrail%2Fusersguide.html%23layouts&sref=rss">Brail</a>. Yes, it&#8217;s that simple.</p>
<p>Often times, we want to inject some view data into our layout, such as the page title. Let&#8217;s say our controller was sending a PageTitle to the view:</p>
<pre class="brush: c-sharp; ">

public void Simple()
{
PropertyBag[&quot;PageTitle&quot;] = &quot;Still a simple sample&quot;;
}
</pre>
<p>How would we get this injected into the <code>title</code> tag in our layout? The quick way is to reference your viewdata, as shown below.</p>
<pre class="brush: html; ">

&lt;head&gt;
&lt;title&gt;$!{PropertyBag[&quot;PageTitle&quot;]}&lt;/title&gt;
&lt;/head&gt;
</pre>
<p>The syntax is similar to that of NVelocity (even the use of the exclamation point to suppress the rendering of <code>null</code>s), but we&#8217;ve got to explicitly reference the PropertyBag. Interestingly, you can also write <code>$!{ViewData["PageTitle"]}</code> &#8212; to Spark, both <code>PropertyBag</code> and <code>ViewData</code> are the same. There are two disadvantages with this method:</p>
<ol>
<li>It&#8217;s not as clean as <code>$!{PageTitle}</code>, which we can do in NVelocity.</li>
<li>The result is not strongly-typed, which limits your ability to work easily with complex types.</li>
</ol>
<p>The solution to these two problems is to specify a Spark <code>viewdata</code> tag and declare the name and type of the view data objects you want to expose. Note the changes in the code below.</p>
<pre class="brush: html; ">

&lt;viewdata PageTitle=&quot;string&quot; /&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;$!{PageTitle}&lt;/title&gt;
&lt;/head&gt;
</pre>
<p>Much cleaner! The Spark web site has <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fsparkviewengine.com%2Fdocumentation%2Fvariables%23Usingviewdata&sref=rss">an example which shows the use of a strongly-typed collection</a> that illustrates this further.</p>
<p>That&#8217;s it for now. It&#8217;s just a quick start, but there&#8217;s a lot more to Spark, and I highly recommend it to MonoRail users.</html></p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=335&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/01/29/simple-layouts-with-spark-and-monorail/' addthis:title='Simple layouts and views with Spark and MonoRail ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/01/29/simple-layouts-with-spark-and-monorail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Spark View Engine (and a brief history of web programming)</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 18:44:27 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=334</guid>
		<description><![CDATA[<p> I should have gone to bed early last night (after all, I did wake up at 4:30AM yesterday morning). Unfortunately, a spark of inspiration quite literally kept me up later than normal sleep cycles dictate… but <a href="http://dev.dejardin.org/">what a spark it was</a>.</p>
<p>To understand the spark, you have to understand my history of server-side web programming.</p>
<p><strong>In the beginning</strong></p>
<p>My first experience with server-side web programming was with <a href="http://en.wikipedia.org/wiki/ColdFusion">Allaire ColdFusion</a> some ten years ago. What was great about ColdFusion is its use of a tag-based scripting language, <a href="http://en.wikipedia.org/wiki/ColdFusion_Markup_Language">CFML</a>, as illustrated below.</p>
<blockquote>
&#60;cfset value = "Hello" /&#62;
&#60;cfoutput&#62;
   #value# Bob!
&#60;/cfoutput&#62;
</blockquote>
<p>After ColdFusion, I came across what is now known as “<a href="http://en.wikipedia.org/wiki/Active_Server_Pages">Classic ASP</a>” – and my web code looked less and less like HTML.</p>
<blockquote>
&#60;% value = "Hello" %&#62;
&#60;%= value %&#62; Bob!
</blockquote>
<p>Classic ASP felt more like programming, less like web programming. Quite accurately, it was web programming via scripting. I missed the tag-based syntax of ColdFusion.</p>
<p>Then there was <a href="http://www.asp.net">ASP.Net</a>, which at first glance <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/">The Spark View Engine (and a brief history of web programming)</a></span>]]></description>
			<content:encoded><![CDATA[<p><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="spark" src="http://www.sidesofmarch.com/wp-content/uploads/spark.jpg" border="0" alt="spark" width="142" height="142" align="right" /> I should have gone to bed early last night (after all, I did wake up at 4:30AM yesterday morning). Unfortunately, a spark of inspiration quite literally kept me up later than normal sleep cycles dictate… but <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fdev.dejardin.org%2F&sref=rss">what a spark it was</a>.</p>
<p>To understand the spark, you have to understand my history of server-side web programming.</p>
<p><strong>In the beginning</strong></p>
<p>My first experience with server-side web programming was with <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FColdFusion&sref=rss">Allaire ColdFusion</a> some ten years ago. What was great about ColdFusion is its use of a tag-based scripting language, <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FColdFusion_Markup_Language&sref=rss">CFML</a>, as illustrated below.</p>
<blockquote>
<pre>&lt;cfset value = "Hello" /&gt;
&lt;cfoutput&gt;
   #value# Bob!
&lt;/cfoutput&gt;</pre>
</blockquote>
<p>After ColdFusion, I came across what is now known as “<a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FActive_Server_Pages&sref=rss">Classic ASP</a>” – and my web code looked less and less like HTML.</p>
<blockquote>
<pre>&lt;% value = "Hello" %&gt;
&lt;%= value %&gt; Bob!</pre>
</blockquote>
<p>Classic ASP felt more like programming, less like web programming. Quite accurately, it was web programming via scripting. I missed the tag-based syntax of ColdFusion.</p>
<p>Then there was <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.asp.net&sref=rss">ASP.Net</a>, which at first glance looked like it was a return to tag-based web programming! Well, almost… along came this weird codebehind model, and the entire stateless request/response nature of the web was bastardized.</p>
<blockquote>
<pre>&lt;asp:label runat="server" id="greeting" /&gt; Bob!
// and in the codebehind
greeting.Text = "Hello";</pre>
</blockquote>
<p>And so there I was until I found <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail&sref=rss">Castle Monorail</a>, which returned web programming to its core, thanks to a fantastic MVC architecture. With MonoRail, you injected text into your web pages using a template engine. For me (and most other newcomers to MonoRail), that meant NVelocity.</p>
<blockquote>
<pre>${value} Bob!
// and in the controller
PropertyBag["value"] = "Hello";</pre>
</blockquote>
<p>Now assignments happened somewhere detached from the view (i.e. the controller), and my web page was looking rather clean. I still yearned for the tag-style days of ColdFusion.</p>
<p>Enter <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fdev.dejardin.org%2F&sref=rss">Spark</a>, a tag-based view engine for MVC platforms (i.e. MonoRail!). Spark gives you almost all the power of C# and the .Net Framework within your view, but does it using a syntax that is entirely tag-based, as shown in the following example from the Spark web site.</p>
<blockquote>
<pre>&lt;var names="new [] {'alpha', 'beta', 'gamma'}"/&gt;
&lt;for each="var name in names"&gt;
  &lt;test if="name == 'beta'"&gt;
    &lt;p&gt;beta is my favorite.&lt;/p&gt;
    &lt;else/&gt;
    &lt;p&gt;${name} is okay too I suppose.
  &lt;/test&gt;
&lt;/for&gt;</pre>
</blockquote>
<p>With this, the presentation logic in your view <em>looks like HTML</em>. It makes the resulting view file much more semantic otherwise.</p>
<p>The Spark view engine is an impressive effort by <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwhereslou.com%2F&sref=rss">Louis DeJardin</a>, who deserves kudos for his efforts. Not only does the code work great, he’s got plenty of documentation on how to get it working with Castle MonoRail – even plenty of references to Windsor integration.</p>
<p>I’m switching a major project from NVelocity to Spark, and plan to blog more about this in the future. In the meantime, <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fdev.dejardin.org%2F&sref=rss">check out Spark</a> – there’s lots to read and learn, and the examples available in the Subversion repository are fantastic.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=334&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/' addthis:title='The Spark View Engine (and a brief history of web programming) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/01/28/the-spark-view-engine-and-a-brief-history-of-web-programming/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Google AdSense ViewComponent for MonoRail</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 02:05:21 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[ViewComponent]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=253</guid>
		<description><![CDATA[<p>Like many, I use <a href="http://www.google.com/adsense/">Google AdSense</a> to host ads on my web sites. Including the ad code typically requires you to paste a block a block of JavaScript onto the page. I don&#8217;t mind injecting the JavaScript, but I wanted to come up with a better way.</p>
<p>The solution I came up with was a quick <a href="http://www.castleproject.org/monorail">MonoRail</a> <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/viewcomponents.html">ViewComponent</a>, AdUnit. It has a simple purpose: to render a block of code for an ad unit of a given size.</p>
<p>The first task was to create an enum which represented all the possible ad unit sizes, based on <a href="https://www.google.com/adsense/static/en_US/AdFormats.html">what&#8217;s offered by Google</a>.</p>


/// &#38;lt;summary&#38;gt;
/// A typical ad unit size, using standards defined by Google.
/// &#38;lt;/summary&#38;gt;
public enum AdUnitSize
{
/// &#38;lt;summary&#38;gt;
/// 728x90
/// &#38;lt;/summary&#38;gt;
Leaderboard = 1,
/// &#38;lt;summary&#38;gt;
/// 468x60
/// &#38;lt;/summary&#38;gt;
Banner = 2,
/// &#38;lt;summary&#38;gt;
/// 234x60
/// &#38;lt;/summary&#38;gt;
HalfBanner = 3,
/// &#38;lt;summary&#38;gt;
/// 125x125
/// &#38;lt;/summary&#38;gt;
Button = 4,
/// &#38;lt;summary&#38;gt;
/// 120x600
/// &#38;lt;/summary&#38;gt;
Skyscraper = 5,
/// &#38;lt;summary&#38;gt;
/// 160x600
/// &#38;lt;/summary&#38;gt;
WideSkyscraper = 6,
/// &#38;lt;summary&#38;gt;
/// 180x150
/// &#38;lt;/summary&#38;gt;
SmallRectangle = 7,
/// &#38;lt;summary&#38;gt;
/// 120x240
/// &#38;lt;/summary&#38;gt;
VerticalBanner = 8,
/// &#38;lt;summary&#38;gt;
/// 200x200
/// &#38;lt;/summary&#38;gt;
SmallSquare = 9,
/// <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/">Google AdSense ViewComponent for MonoRail</a></span>]]></description>
			<content:encoded><![CDATA[<p>Like many, I use <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.google.com%2Fadsense%2F&sref=rss">Google AdSense</a> to host ads on my web sites. Including the ad code typically requires you to paste a block a block of JavaScript onto the page. I don&#8217;t mind injecting the JavaScript, but I wanted to come up with a better way.</p>
<p>The solution I came up with was a quick <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail&sref=rss">MonoRail</a> <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Fviewcomponents.html&sref=rss">ViewComponent</a>, AdUnit. It has a simple purpose: to render a block of code for an ad unit of a given size.</p>
<p>The first task was to create an enum which represented all the possible ad unit sizes, based on <a href="http://redirectingat.com?id=17923X751173&xs=1&url=https%3A%2F%2Fwww.google.com%2Fadsense%2Fstatic%2Fen_US%2FAdFormats.html&sref=rss">what&#8217;s offered by Google</a>.<span id="more-253"></span></p>
<pre class="brush: c-sharp; ">

/// &amp;lt;summary&amp;gt;
/// A typical ad unit size, using standards defined by Google.
/// &amp;lt;/summary&amp;gt;
public enum AdUnitSize
{
/// &amp;lt;summary&amp;gt;
/// 728x90
/// &amp;lt;/summary&amp;gt;
Leaderboard = 1,
/// &amp;lt;summary&amp;gt;
/// 468x60
/// &amp;lt;/summary&amp;gt;
Banner = 2,
/// &amp;lt;summary&amp;gt;
/// 234x60
/// &amp;lt;/summary&amp;gt;
HalfBanner = 3,
/// &amp;lt;summary&amp;gt;
/// 125x125
/// &amp;lt;/summary&amp;gt;
Button = 4,
/// &amp;lt;summary&amp;gt;
/// 120x600
/// &amp;lt;/summary&amp;gt;
Skyscraper = 5,
/// &amp;lt;summary&amp;gt;
/// 160x600
/// &amp;lt;/summary&amp;gt;
WideSkyscraper = 6,
/// &amp;lt;summary&amp;gt;
/// 180x150
/// &amp;lt;/summary&amp;gt;
SmallRectangle = 7,
/// &amp;lt;summary&amp;gt;
/// 120x240
/// &amp;lt;/summary&amp;gt;
VerticalBanner = 8,
/// &amp;lt;summary&amp;gt;
/// 200x200
/// &amp;lt;/summary&amp;gt;
SmallSquare = 9,
/// &amp;lt;summary&amp;gt;
/// 250x250
/// &amp;lt;/summary&amp;gt;
Square = 10,
/// &amp;lt;summary&amp;gt;
/// 300x250
/// &amp;lt;/summary&amp;gt;
MediumRectangle = 11,
/// &amp;lt;summary&amp;gt;
/// 336x280
/// &amp;lt;/summary&amp;gt;
LargeRectangle = 12
}
</pre>
<p>With that, I now can write the <code>AdUnitComponent</code>.</p>
<pre class="brush: c-sharp; ">

/// &amp;lt;summary&amp;gt;
/// Displays an ad unit of a given size.
/// &amp;lt;/summary&amp;gt;
[ViewComponentDetails(&quot;AdUnit&quot;)]
public class AdUnitComponent : ViewComponent
{
private string _adUnitCode;
private bool _renderView;

/// &amp;lt;summary&amp;gt;
/// The HTML ID of the form element to set focus to.
/// &amp;lt;/summary&amp;gt;
[ViewComponentParam(Required = true)]
public virtual AdUnitSize Size { get; set; }

/// &amp;lt;summary&amp;gt;
/// Initializes the view component.
/// &amp;lt;/summary&amp;gt;
public override void Initialize()
{
_adUnitCode = WebConfigurationManager.AppSettings[&quot;AdUnit_&quot; + this.Size.ToString()];
_renderView = !String.IsNullOrEmpty(_adUnitCode);
}

/// &amp;lt;summary&amp;gt;
/// Renders the view component, injecting HTML into the web page.
/// &amp;lt;/summary&amp;gt;
public override void Render()
{
if (_renderView)
{
RenderText(_adUnitCode);
}
CancelView();
}
}
</pre>
<p>The most important code takes place in the <code>Initialize</code> method. It&#8217;s in that method where we retrieve the ad code from the web.config file. Simply add an entry to your <code>&lt;appSettings&gt;</code> section with the name <code>AdUnit_<em>AdUnitSize</em></code>. An example follows.</p>
<pre class="brush: xml; ">

&amp;lt;appSettings&amp;gt;
&amp;lt;!-- ad units --&amp;gt;
&amp;lt;add key=&quot;AdUnit_MediumRectangle&quot; value=&quot;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;&amp;lt;!--
google_ad_client = &quot;pub-4509135091166921&quot;;
google_ad_slot = &quot;2912665903&quot;;
google_ad_width = 300;
google_ad_height = 250;
//--&amp;gt;
&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&amp;gt;
&amp;lt;/script&amp;gt;
&quot; /&amp;gt;
&amp;lt;add key=&quot;AdUnit_WideSkyscraper&quot; value=&quot;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;&amp;lt;!--
google_ad_client = &quot;pub-4509135091166921&quot;;
google_ad_slot = &quot;3455950222&quot;;
google_ad_width = 160;
google_ad_height = 600;
//--&amp;gt;
&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&amp;gt;
&amp;lt;/script&amp;gt;
&quot; /&amp;gt;
&amp;lt;/appSettings&amp;gt;
</pre>
<p>You don&#8217;t have to use Google AdSense code &#8212; you can actually use this ViewComponent to inject any HTML into a page.</p>
<p>Last thing &#8212; how do you call the <code>AdUnitComponent</code> from your view? If using NVelocity&#8230;</p>
<pre class="brush: html; ">

#component(AdUnit with &quot;Size=WideSkyscraper&quot;)
</pre>
<p>That would render the text specified in the value field of the <code>AdUnit_WideSkyscraper</code> key in your configuration file&#8217;s appSettings.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=253&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/' addthis:title='Google AdSense ViewComponent for MonoRail ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

