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.

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