Spark View Engine on WebForms
Spark view engine is a neat little web utility that allows you to write code like this:
<viewdata products="IEnumerable[[Product]]"/> <ul if="products.Any()"> <li each="var p in products">${p.Name}</li> </ul> <else> <p>No products available</p> </else>
If your eye skipped over it because it looks just like html I wouldn’t blame you. But look again. Loops and conditions are nicely embedded in html. Spark really does let the html dominate (which can be a good thing). These are but a few of all the nice features. If you have them, spend a couple of minutes browsing the spark documentation to learn what’s more.
To learn a bit about the inner workings of this wonderful tool I set out to add WebForms support. The spark codebase has a very testable design and can be coerced to help out in a WebForms control without modification. The end result look like this:
<spark:View runat="server"> <ul if="Container.GetProducts().Any()"> <li each="var p in Container.GetProducts()">${p.Name}</li> </ul> <else> <p>No products available</p> </else> </spark:View>
Download and usage
To use it download
and unzip into your bin folder.
Register the control in web.config:
<pages> <controls> <add tagPrefix="spark" namespace="Spark.Web.Forms" assembly="Spark.Web.Forms"/>
Add something useful to iterate over to your page or user control:
public string[] Values; protected void Page_Load(object sender, EventArgs e) { Values = new string[] { "Simple", "As", "Pancake" }; }
And add the spark control:
<spark:View runat="server"> <ul> <li each="var c in Page.Values">${c}</li> </ul> </spark:View>
Future improvement ideas
Some features are definitely missing (and they might require modification of the spark code itself):
- Spool content from a spark view controls placed later in the control tree
- Cache the compiled views
- Support the spark cache element
The code is over here.
Comments