How to Customize Module Builder Widget Templates with Conditional Statements

by Gregg Crystal Dec 12, 2016

Module Builder is by far one of the best features of Sitefinity. Not only do you get to create a database through a nice user interface, but you get to edit the presentation of the data with the Widget Template editor.

There's a lot to learn with this feature, but the one item I want to focus on today is creating conditional statements (if-then-else logic).

You may want to change entire pieces of HTML based on the property of a module. Sitefinity allows this with the sf:condition tag, which works like so:

<sf:conditional if='<%# Eval("ProductType").ToString()=="build-to-spec" %>' runat="server">
   <then>

   </then>
   <else>

   </else>
</sf:conditional>

The trick to get this to work is the Eval statement. The Eval syntax is straight .NET WebForms, nothing really to do with Sitefinity here. The ProductType is a custom field on the module that is defined as a multiple choice, like so:

Sitefinity-Module-MultipleChoice-Type

Note that you specify the choice values in Module Builder, and here it's 'off-the-shelf' or 'build-to-spec'. You just have to ToString() the Eval statement to be able to do a comparison.

Within the then and else tags you can just put HTML.

New Call-to-action