<?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; ViewComponent</title>
	<atom:link href="http://www.sidesofmarch.com/index.php/archive/tag/viewcomponent/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>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>

