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.

No comments: