Razor Templating Tricks with LINQ

Often times when folks are stepping into the world of the Razor Mediator they do not fully understand what new capabilities are provided OOTB. Back in the days where DWT reigned supreme, more often than not, we had to write TBBs for simple logic such as grouping component presentations for rendering, etc.

Using the Razor Mediator, you can actually cut out a lot of the simple TBBs bloating the project and keep the codebase clean. Here are a couple of examples which I have been using for rendering out component presentations (which was previously done through C# grouping TBBs which created component arrays based on template titles) which seem simple but are often overlooked. The following are some simple LINQ tricks I’ve been using to render CPs, but the same principles can be applied to other templating logic.

Example #1:

We have a few variations of templates which are all named with a variation of “Widget”; ie “Widget [JSP Tag]”, “Widget [DCP], etc. Lets say we wanted to render out any variation of these templates in the same code-block. We can actually run a LINQ query on the ComponentPresentations list where the CPs template title contains  the string “Widget”.

@foreach (var cp in ComponentPresentations.Where(c => c.Template.Title.Contains("Widget"))
{
@cp.renderComponentPresentation();
}

Example #2:

We can also extend the logic to render out the first “Widget” component presentation (CPs where the template contains ‘Widget’) that are allowed on the page (marked as allowed on the page using a dynamic assembly).

@foreach (var cp in ComponentPresentations.Where(c => c.Template.Title.Contains("Widget") && c.Template.AllowOnPage && c.IsFirst)
{
@cp.renderComponentPresentation();
}


Conclusion:

One final thing to keep in mind is how reusable is the code being written in a template? If it’s something reused consistently across the site, you might want to consider moving it to a Razor helper function which can be called globally. The key is to determine where the code is best suited to live; either in the template itself (minimal logic & code), a razor helper TBB (reusable globally & extends templating functionality) or a C# TBB (other use cases).

The aforementioned tricks are in no means an attempt to cut out TBBs; the objective is to cut out TBBs which have simple enough logic that it can be migrated into the templates themselves. Striking the right balance is the key to writing clean & concise templates with maximum re-usability.

Shoutout to Alex Klock for the mediator and continued support.

References:

  • Download the Razor Mediator here, and read the documentation here.
  • Read Alex Klocks blog here.
  • Will Prices Grouping TBB that I mentioned can be found here.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>