NHaml Discussion Group

Posted by Andrew on September 18, 2008

Using NHaml? Then head on over to .

Thanks to for setting it up.

NHaml 1.3 Released

Posted by Andrew on September 05, 2008

Updated to support ASP.NET MVC Preview 5.

As always, get it .

Joining Microsoft

Posted by Andrew on September 02, 2008

I’m very excited to announce that, starting November, I’ll be working as a dev within the SQL Server DP group - specifically on the .

The position is based in Redmond but I missed the H1B this year and so we’re heading up to the . By all accounts, Vancouver is a great place to live and my wife and I are looking forward to the change of scene - We both ski too and are stoked about having one of the right on our doorstep :-)

The decision to leave , the company I co-founded was, of course, not taken lightly. I’m highly proud of what I helped create there - especially LightSpeed (my baby originally). I will particularly miss the variety of discussion that occurs in a small, developer-led shop like Mindscape. In my opinion, starting a company is a truly worthwhile experience because it forces you to consider all those things other than the code - You quickly learn that it’s those things that matter more.

That said, working for a company like Microsoft gives you the opportunity to touch many, many people’s lives…and, hopefully, make them just a little bit better.

NHaml: Block Methods and RESTful Helpers

Posted by Andrew on August 21, 2008

New in the are a couple of cool features worth mentioning:

Block Methods

Block Methods are helper methods that accept a block of NHaml markup as a final argument.

An example (albeit contrived) to demonstrate:

- Html.Tag("div") () =>
  Some regular NHaml markup here...

which results in:

<div>
  Some regular NHaml markup here...
</div>

And the Tag method looks like this:

public void Tag(string name, Action yield)
{
  _output.WriteLine("<" + name + ">");
  yield();
  _output.WriteLine("</" + name + ">");
}

As we can see, NHaml wraps up the nested block markup into a lambda (or anonymous method if targeting C# 2.0) and passes a delegate to the block as the last argument of the helper method. Invoking the delegate causes the markup within the block to be rendered. Notice too, we can write directly to the output stream: _output is an instance of NHaml’s IOutputWriter which is passed to our helper class when it’s instantiated. It has the following methods:

void WriteLine(string value);
void Write(string value);
void Indent();
void Outdent();

Output written using these methods will be correctly indented and we can also further control the indent level if we need to write nested output.

Worth mentioning is that we can also pass arguments to a block:

- Html.Tag("div", 21, 21) (i, j) =>
  = "N was: " + (i + j)

which yields:

<div>
  N was: 42
</div>

And our helper method:

public void Tag<T1, T2>(string name, T1 t1, T2 t2, Action<T1, T2> yield)
{
  _output.WriteLine("<" + name + ">");
  yield(t1, t2);
  _output.WriteLine("</" + name + ">");
}

RESTful Helpers

Using a Block Method we can create a nice Rails-style helper that encapsulates building a form for a single model:

- var product = new Product { ProductName="Soap on a Rope" }
- Html.Form(product) (f) =>
  = f.Label(p => p.ProductName)
  = f.TextField(p => p.ProductName)
  %br
  = f.Label(p => p.UnitPrice)
  = f.TextField(p => p.UnitPrice)
  %p
    = Html.SubmitButton()

Results in:

<form action="/products.mvc/create" method="post">
  <label for="Product.ProductName">Product Name</label>
  <input type="text" name="Product.ProductName" id="Product.ProductName" value="" />
  <br />
  <label for="Product.UnitPrice">Unit Price</label>
  <input type="text" name="Product.UnitPrice" id="Product.UnitPrice" value="" />
  <p>
    <input type="submit" />
  </p>
</form>

A couple of things to note here:

  1. Our Form method takes a model object and uses the conventions of REST to generate the target action. To make this work our model needs to implement a small interface: IModel:
    public interface IModel
    {
      int Id { get; }
      bool IsNew { get; }
    }
  2. Happily, we can bind our model attributes in a strongly typed fashion using Expressions.

Here’s what our Form method looks like:

public virtual void Form<TModel>(TModel model, Action<FormBuilder<TModel>> yield)
  where TModel : IModel
{
  // this should be pluggable
  var formBuilder = new FormBuilder<TModel>(model, this, _outputWriter);
 
  formBuilder.OpenTag(FormMethod.Post, new RouteValueDictionary());
  yield(formBuilder);
  formBuilder.CloseTag();
}

RESTful Link Helpers

Finally, we can apply RESTful conventions to link helpers too. Here are a few examples of some NHaml ActionLink helpers that work using REST conventions.

= Html.ActionLink<CategoriesController>() // /categories
= Html.ActionLink<CategoriesController>(RestfulAction.New, "Add Category") // /categories/new
= Html.ActionLink(category)  // /categories/show/42
= Html.ActionLink(category, RestfulAction.Edit, "Edit Category") // /categories/edit/42
= Html.ActionLink(category, RestfulAction.Destroy, "Delete Category") // /categories/destroy/42

Thoughts/comments appreciated.

VS File Explorer

Posted by Andrew on August 13, 2008

We at just released a cool, free VS add-in: the Visual Studio File Explorer. It’s basically Windows Explorer inside Visual Studio 2008 so you can perform file system tasks without needing to context switch. It’s also shell enabled so extensions like TortoiseSVN work too.