We’ve recently started using Spark View Engine
for some ASP.NET MVC projects here at Logos. To me, the syntax feels cleaner
than ASP.NET views, probably because it’s more markup and less embedded code.
One thing that bugged me was that in order to use MVC form helpers I had to go
back to embedding code.
Some searching of the Internet revealed I wasn’t the only one
who found that unpalatable. Unfortunately, the only suggested solution, using
Spark’s completely undocumented bindings feature, doesn’t actually work. While
reading the Spark code (man, I love open source) I found that there is an
extension mechanism. I read some more code and figured out how I could use it
to make a “using” extension that would allow me to avoid embedding code when I
want to use using.
What I want is an element like this:
When the Spark compiler encounters a logos:using element node, it should
replace that node with a statement node that generates the correct “using” C#
code and begin a code block. When the compiler encounters the end element, it
should close the block. The following extension accomplishes this.
UsingExtension.cs:
Now we just need to tell the SparkViewEngine how to use our extension. To do
that we need a factory.
LogosSparkExtensionFactory.cs:
This is what my Application_Start() looks like:
What I’ve presented here is actually a simplified version of what I ended up
with. I extended UsingExtension with FormExtension, which has attributes for
setting up the form and results in a view file that looks even more like
markup and less like code. I also created some other extensions for other
helpers.
Now we have a small library of commonly used custom controls, of a sort, to
help keep our presentation layer looking presentable.