Archive for October, 2011

Converting Brail View Engine pages on MonoRail to use the Spark View Engine

Converting from the Brail View Engine to the Spark View Engine for MonoRail apps might not be interesting to many people, but here it is nonetheless. (We’re having to convert a project over due to switching our dependency management to Nuget.. hence the old Brail View Engine does not like the newer Castle.Core.)

Tags:

Brail property bag shortcuts:

<head>
<title>${Title}</title>
</head>

Spark equivalent :

<head>
<title>${PropertyBag["Title"]}</title>
</head>

The only problem with this is it’s not as clean as the Brail version. However, there’s a solution. By using the

viewdata

tag, you can write the code just like Brail:

<viewdata SiteRoot='string' />
<head>
<title>${Title}</title>
</head>

Conditionals:

Brail:

<% if AppEnvironment.ToUpper() == "DEV": %>
<div>
V: <strong>${VersionNumber}</strong>
Environment: <strong>${AppEnvironment}</strong>
Connection: <strong>${ConnectionEnvironment}</strong>
<% if AreThereProblemsWithTheEnvironment: %>
<a href="${siteroot}/Admin/Health.rails">
<img src="${siteroot}/Images/sick.jpg" title="I'm not feeling so hot...there are some problems with the environment" />
</a>
<% end %>
</div>
<% end %>

Spark:

<if condition='PropertyBag["AppEnvironment"].ToString().ToUpper() == "DEV"'>
<div>
V: <strong>${PropertyBag["VersionNumber"]}</strong>
Environment: <strong>${PropertyBag["AppEnvironment"]}</strong>
Connection: <strong>${PropertyBag["ConnectionEnvironment"]}</strong>
<if condition='(int)PropertyBag["AreThereProblemsWithTheEnvironment"] > 0' >
<a href='${SiteRoot}/Admin/Health.spark'>
<img src="${SiteRoot}/Images/sick.jpg" title="I'm not feeling so hot...there are some problems with the environment" />
</a>
</if>
</div>
</if>

Looping:

Brail:

<div>
<%
addSlash = false
for breadCrumb in breadCrumbs:
output " > " if addSlash
%>
<a href="${breadCrumb.Href}">${breadCrumb.Name}</a>
<%
addSlash = true
end
%>
</div>

Spark:

<div>
<var addSlash="false" type="bool">
<for each='var breadCrumb in (List[[BreadCrumb]])PropertyBag["BreadCrumbs"]'>
<span if='addSlash == true'>></span>
<a href='${breadCrumb.Href}'>${breadCrumb.Name}</a>
# addSlash = true;
</for>
</var>

Partial Pages:

In Brail, the way we were using partial pages was to specify the partials as “layouts” after the main layout on our controller:


[Layout("Main", "myPartialPage")]
public class HomeController : SmartDispatcherController

{

// ...

}

and in the Main layout, you specify where the view content is rendered like so:

<div>
${ChildOutput}
</div>

This doesn’t work in Spark. If you provide a second layout parameter to the Layout attribute on your controller, Spark will use that for the view content. So to fix this, we have to remove the second Layout attribute parameter:


[Layout("Main")]
public class HomeController : SmartDispatcherController

{

// ...

}

and then adjust our HTML to have the <use /> tag instead (I think this is cleaner)

<div>
<use file="myPartialPage" />
<use content="view" />
</div>

Other things:
Brail has a handy ${SiteRoot} variable added to the view context, which will give you the root path of the site for things like including scripts and css. With Spark, you can also use ${SiteRoot}, you just have to setup a couple of things first:


[Layout("Main")]
public class HomeController : SmartDispatcherController
{
public override void Initialize()
{
base.Initialize();
PropertyBag["SiteRoot"] = Context.ApplicationPath;
}
}

and add the viewdata tag at the top of the page:

<viewdata SiteRoot='string' />

now you can reference scripts, urls, css, etc. properly:

<script type="text/javascript" src="${SiteRoot}/Scripts/jquery-1.4.3.min.js"></script>
Advertisements

Leave a comment