Spark in practice

, , , No Comments »

I have to say I’m really happy with how the view engine turned out. It’s being dog fooded on some prototyping at the moment and there are lots of things that I really appreciate as it’s going along.

Consider the following partial file that renders a tab strip followed by a number of pages. Each page has a number of items…

_MostPopular.spark

<div id="MostPopular">

  <content name="head">
    <link rel="stylesheet" type="text/css"
          href="${SiteRoot}/content/css/home-mostpopular.css"/>
    <script type="text/javascript"
            src="${SiteRoot}/content/js/home-mostpopular.js"></script>
  </content>

  <div class='header'>Most Popular</div>

  <ul class='tabs'>
    <var i="0">
      <li each="var page in mostPopularData.Pages"
          id='MostPopularTab${++i}'
          class='tab ${i == 1 ? "selected" : ""}'>
        <a href='#'>${page.Title}</a>
      </li>
    </var>
  </ul>

  <div class='pages'>
    <var i="0">
      <ul each="var page in mostPopularData.Pages"
          id='MostPopularTab${++i}Page'
          class='page ${i == 1 ? "selected" : ""}'>
        <li each="var item in page.Items.Take(5)">
          <a href="${item.AbsoluteUrl}">${H(item.Headline)}</a>
        </li>
      </ul>
    </var>
  </div>

</div>

Read the rest of this entry »

Spark learns a few new tricks

, , , , , , 10 Comments »

The folks at the Castle Project development list have provided some excellent constructive criticism and some new functionality was added to Spark as a result.

One thing which is new is the ability to inline code directly.

#int total = 0;
#foreach(var item in items)
#{
#   total += item.Quantity;
#}

#if (Params["page"] == "2") {
  <p>Second Page - total: $total;</p>
#} else {
  <p>Not the second page
#}

Or using the slightly more standard <% %> syntax

<%
int total = 0;
foreach(var item in items)
{
  total += item.Quantity;
}
%>

<%if (Params["page"] == "2") {%>
  <p>Second Page - total: <%=total%> </p>
<% } else { %>
  <p>Not the second page
<% } %>

Another question which came up was about the support for MonoRail ViewComponent classes. This was an especially interesting point because the view engine itself is written to be used in any framework, while the ViewComponent is very specific to the Castle Project. To make this happen the Spark parser and code generator was changed to call an extension factory interface as it processed the elements from the view templates it digested. This enabled the MonoRail-specific view engine library to recognize elements whose names matched those of the registered view components. It can then provide an extension that consume the contents of that element to write it’s own chunks of generated code at the appropriate point.

So the net result is support for things like this.

<p>blah blah blah</p>
<DiggStylePagination Page="SomeMessages" Adjacents="3">
  <prev>Go Back</prev>
  <next>Show More</next>
</DiggStylePagination>
<p>blah blah blah</p>

And another piece of functionality which was added was the ability to declare and use helper-like functions inside the template file.

<macro name="MessageBox" caption="string" message="string">
  #Logger.Info(message);
  <div class="msgbox">
    <h3>$H(caption);</h3>
    <div>$message;<div>
  </div>
</macro>

<p>blah blah blah</p>
$MessageBox("Warning",
  "You're about to change " + somearg + " permanently.");
<p>blah blah blah</p>

The thing that I think is pretty cool about that is how it create a function what works like a helper method, because it takes a handful of arguments and returns a string, but the body of this thing is declared in the template vernacular. So it can be very “view oriented” in how it’s build and maintained. Which makes sense for a lot of helper-style methods which are very much about the idea of concatinating and returning html – implementing simple helper methods in classes can occastionally put more html in your cs files than you’re comfortable with.

In any case I’m really happy with how the view engine has come together. It’s been a delightful experience, didn’t take much actual time, and has really reinforced for me the value of tools like resharper and practices like test driven development.

Spark view engine – coming soon

, , , , , , No Comments »

Between GTA IV and working on the Spark view engine my blogging time on nights and weekends has been nearly squeezed out of existence.

So in between “real” posts here’s a quick preview about what’s coming up… Some syntax from the Spark view engine soon to be available to Asp.Net Mvc and Castle Project MonoRail.

If you’re especially curious there are some version 0.1 docs that show syntax.

And here are some fragments from a monorail demo site for an example.

homecontroller.cs

	[Layout("Default")]
	public class HomeController : SmartDispatcherController
	{
		public void Index()
		{

		}

		public void Apply(string name, string address)
		{
			PropertyBag["caption"] = "Information submitted";
			PropertyBag["message"] = string.Format("Name {0}, Address {1}", name, address);
			PropertyBag["continue"] = new {controller="Home", action="Index"};
			RenderSharedView("common/simplemessage");
		}
	}

home/index.xml

<h1>Hello world</h1>

<div>
  $Form.FormTag(new {action="apply", method="get"});
  <p>$DateTime.UtcNow;</p>
  <p>Name: $Form.TextField("name");</p>
  <p>
    Address:
    $Form.TextField("address", new {@class="w00t", style="margin-left:20px"});
  </p>
  <p>$Form.Submit("Apply");</p>
  $Form.EndFormTag();
</div>

common/simplemessage.xml

<div>
  <h3>$H(ViewData["caption"]);</h3>
  <var link='Url.Link("Continue", ViewData["continue"])'/>
  <p>$link;</p>
  <p>$H(ViewData["message"]);</p>
  <p>$link;</p>
</div>

shared/default.xml

<html>
  <head>
    <use namespace="System"/>
    <use namespace="Castle.MonoRail.Views.Spark"/>
    <title>Hello</title>
    $Ajax.InstallScripts();
  </head>
  <body>
    <div id="header">
    </div>
    <div id="main">
      <use content="view"/>
    </div>
    <div id="footer">
    </div>
  </body>
</html>

Zu Zu and his new bread buddy

, , No Comments »

Zu Zu\'s bread friend

Zu Zu’s bread friend.

This is our dog which is least sane.

Dethklok

, No Comments »

It’s been a little while since the last post.

The next techie one will be about the Spark view engine I’ve put together for Asp.Net Mvc. But the current geekie one is about Dethklok! The tour is coming to First Ave in Minneapolis and Brenda and I just bought tickets for it.

Dethklok is looking forward to ruining your hometowns. Brutal.

Gotta love Adult Swim.

Design by j david macor.com.Original WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in