I ❤️ higher order functions and generally refuse to write non recursive looping structures. With Razor though I didn't have a great way of displaying lists without looping.
###Usage
The following code will render the _link
partial for every item in the links
list. It is basically a functor from data to output given a template.
@Html.RenderPartials("_link", links)
Without this extension I would have to write something like this
@foreach(var link in links)
{
Html.RenderPartial("_link", link)
}
###Wrapping the partial
In the first example we're using the _link
template as is. But what if we want an html list of links?
This allows us to add any arbitrary html before and after each element.
<ol>
@Html.RenderPartials("_link", link, "<li>", "</li>")
</ol>
Without this extension I would have to write something like this
<ol>
@foreach(var link in links)
{
<li>
@Html.RenderPartial("_link", link)
</li>
}
</ol>
###Then?
Coming from languages that allow you to pipe data through functions this is essentially how I think about building software. Variables and null are the root of most evil and debugging. Using Then
allows me to remove most of the variables from my code. Plus, it's a higher order function and we've already discussed my ❤️ for them :D