About this article

Written by:
Posted:
24/03/2011 08:31:00

About the author

Imar Spaanjaars is the owner of De Vier Koeden, a company specializing in consultancy and development on the Microsoft .NET platform and DynamicWeb.

Interested in custom development or consultancy on DynamicWeb or .NET? Then contact De Vier Koeden through the Contact page.

If you want to find out more about Imar Spaanjaars, check out his About page on this web site or check out his personal website.

Dynamicweb's Best Kept Secrets - If statements

NOTE: the concepts presented in this article are now considered obsolete possibly because better alternatives are available.

Note: Since Dynamicweb 8.2 you can use @Else as well. More details can be found here.

If you've been working with Dynamicweb's template engine, I am sure you're familiar with the If Defined and If Not Defined tags that enable you to execute some server side logic. As a refresher, here's a simple example that displays different content depending on whether the user is logged in or not.

<!--@If Defined(Global:Extranet.UserName)-->
  Logged in
<!--@EndIf(Global:Extranet.UserName)-->
<!--@If Not Defined(Global:Extranet.UserName)-->
  Not logged in
<!--@EndIf(Global:Extranet.UserName)-->

Useful as these tags are, I often had the need for more. For example, switching from one language layer to another would be a lot simpler if you could use something like this:

<!--@If(Global:Area.ID==1)-->
  <a href="Default.aspx?AreaID=2">View this site in English</a>
<!--@EndIf-->
<!--@If(Global:Area.ID==2)-->
  <a href="Default.aspx?AreaID=1">Bekijk de site in het Nederlands</a>
<!--@EndIf-->

This code would compare the current Area ID and then display a link to switch to either the English or Dutch layer, depending on the current value of the Area ID. Clearly, this is just an example implementation. I could come up with many situations where this would be useful.

But guess what? You can already do this in Dynamicweb!

In version 19.2.1.3, improvements have been made to the If statements in templates. This hasn't been announced with a lot of buzz (which it certainly deserves if you ask me) as it was only mentioned briefly in the version history document for that release. So I am writing this short article to show you how to make use of this great new feature.

The new If statements support the following options:

OperatorDescriptionExample
= and ==Equals operator, supporting both VB and C# style syntax.<!--@If(Global:Area.ID==1)--> Area ID is 1 <!--@EndIf-->
<> and !=Not equals operator, supporting both VB and C# style syntax.<!--@If(Global:Area.ID!=1)--> Area ID is NOT 1 <!--@EndIf-->
< > >= <=Comparison operators supporting less than, greater than, less than or equal to, and greater than or equal to.<!--@If(Global:Area.ID>=2)--> Area ID is greater than or equal to 2 <!--@EndIf-->
<contains> <startswith> <endswith>String comparison operators which enable you to match on the beginning, middle or end of a string. Each operator is case insensitive.<!--@If(Global:Area.Primarydomain<startswith>'www')--> We're on the main site<br /> <!--@EndIf--> <!--@If Not(Global:Area.Primarydomain<startswith>'shop')--> We're not in the shop <!--@EndIf-->

Notice how in the last example you see the use of Not to revert the outcome of the startswith check. Not is supported on all If statements. When you'd run the last example on a site such as http://www.somesite.com, you would see:

We're on the main site

We're not in the shop

You would see no output at all if you were browsing to http://shop.somesite.com.

The new If syntax is quite strict so be careful when writing yourcode. For example, surrounding the double equals sign (==) in the first example with spaces would cause this code not to run.

To see more examples of the new If statements, check out If statements page at the documentation portal. Don't accidentally overlook the green "See more code examples" at the bottom which links to a text files with a few more useful examples.

Happy iffing!

By clicking 'Accept All' you consent that we may collect information about you for various purposes, including: Statistics and Marketing