Posts

Showing posts from February, 2010

Spark - Optional Parameters in Partials

While on the topic of the Spark View Engine , there is an easy way to make a parameter of a partial optional so you don't always need to pass a value when calling the partial. Simply define the variable as part of the viewdata property. <!-- samplePartial.spark --> <viewdata optionalParameter="string" /> <a class="active?{optionalParameter == "home"}">Home</a> That's it. Now you can call the partial by itself... <samplePartial /> ... or with the optionalParameter ... <samplePartial optionalParameter="home" /> A helpful trick that I couldn't find a lot about online.

Spark - Recursive rendering of partial files not possible (but it is!)

We've been using the Spark View Engine a lot at work. I absolutely love it. Our front end developers love it (they don't need to re-learn something like we had to for NHaml ) and if they don't want to use Spark's features, standard HTML works without problems. During one project, I was making some contextual menus. I wanted to call a Spark partial recursively in order to not repeat myself & keep things flexible for the future. Unfortunately Spark specifically disallows this. If you try it, you will get an Exception that states "Recursive rendering of partial files not possible" . Well that just sucks. I understand the limitation in the View Engine itself but there is a way around it. Not super elegant but definitely functional without sacrificing anything other than style . I posted the following example in the Spark project itself. The trick is using the HtmlHelper 's RenderPartial method. Let's say we have a partial called: navigation