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.spark.

< --- Contents of: navigation.spark --- >
<viewdata navItem="Models.NavigationItem" />

<li><a if="navItem != null" href="${navItem.Url}">${navItem.Name}</a></li>

<ul if="navItem.Children.Count > 0" >
<for each="var child in navItem.Children" >
#{Html.RenderPartial("_navigation.spark", new { child });}
</for>
</ul>


And the page that you want to list navigation items, simply do:

<viewdata navigationItems="List<Models.Navigation>" />

<navigation for="var nav in navigationItems" navItem="nav" />



Now you can call the Partial recursively without issues.

Comments

Popular posts from this blog

Require comment / message on all SVN commits for Visual SVN Server

Fluent NHibernate - Incorrect syntax near the keyword 'Group'

Deleting Cookies (or Managing Cookie Domains) in ASP.NET