Notes on The Big View Engine Comparison
internet, opensource, programming, spark, tech November 9th, 2010There was another very interesing comparison of several view engines recently. Some of the Spark syntax isn’t exactly how I would have done things, so I thought I would respond with some of those notes. I was going to add a comment, but wanted to be able to preview code samples.
So here are some thoughts.
Displaying Variable Content
Original
${Html.Encode(ViewData["Message"])}
Spark auto-encodes if you enabled that, so you wouldn’t need the Html.Encode.
${ViewData["Messages"]}
You can also declare property accessors to view data dictionary.
<viewdata Messages="string"/>
${Messages}
Conditions
Original
<if condition='DateTime.Now.Hour > 20 || DateTime.Now.Hour < 6'> <p>It's bed time!</p> </if> <else> <p>Party!</p> </else>
There’s yet another way of doing conditions when else/if and else are involved that I kind of prefer, personally.
<test if='DateTime.Now.Hour > 20 || DateTime.Now.Hour < 6'> <p>It's bed time!</p> <else/> <p>Party!</p> </test>
and there’s also inline code support if you prefer
# if(DateTime.Now.Hour > 20 || DateTime.Now.Hour < 6) {
<p>It's bed time!</p>
# } else {
<p>Party!</p>
# }
Loops
Original
<var list='new List<String>() { "WebForms", "Razor", "Spark", "NHaml" }'/>
<ul>
<li each='var item in list'>
${item}
</li>
</ul>
Loops looks pretty good, though (as always) if you prefer to inline code you could also use something like the following
# var list = new List<string>() { "WebForms", "Razor", "Spark", "NHaml" };
<ul>
# foreach (var item in list) {
<li>${item}</li>
# }
</ul>
Displaying a Form
Original
<viewdata model="MvcApplication1.Models.LogOnModel"/>
# using (Html.BeginForm()) {
${Html.ValidationSummary(true, "Login was unsuccessful.")}
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
${Html.LabelFor(m => m.UserName)}
</div>
<div class="editor-field">
${Html.TextBoxFor(m => m.UserName)}
${Html.ValidationMessageFor(m => m.UserName)}
</div>
<div class="editor-label">
${Html.LabelFor(m => m.Password)}
</div>
<div class="editor-field">
${Html.PasswordFor(m => m.Password)}
${Html.ValidationMessageFor(m => m.Password)}
</div>
<div class="editor-label">
${Html.CheckBoxFor(m => m.RememberMe)}
${Html.LabelFor(m => m.RememberMe)}
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
# }
This one is particularly interesting if you use the new bindings feature to create tags for your favorite helpers.
<viewdata model="MvcApplication1.Models.LogOnModel"/>
<Form>
<ValidationSummary ExcludePropertyErrors="true">
Login was unsuccessful.
</ValidationSummary>
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<Label For="UserName"/>
</div>
<div class="editor-field">
<TextBox For="UserName"/>
<ValidationMessage For="UserName"/>
</div>
<div class="editor-label">
<Label For="Password"/>
</div>
<div class="editor-field">
<Password For="Password"/>
<ValidationMessage For="Password"/>
</div>
<div class="editor-label">
<CheckBox For="RememberMe"/>
<Label For="RememberMe"/>
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
</Form>
Conclusions
So – as with all things – each of the view engines will have lots of ways to approach any problem. Just wanted to add some of my own personal preferences to the mix for the Spark column out of band. Thanks again to Shay Friedman for writing up the comparison, and also for his work supporting IronRuby.
November 10th, 2010 at 3:14 am
I’m glad you mentioned spark bindings. What an incredible feature they are. I love them.