About this article

Written by:
Posted:
11/01/2013 11:30: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 Part 2 - Else statements and support for and and or

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

Back in 2011, I blogged about Dynamicweb's support for If statements. This is a great feature that makes your templates much easier to write and maintain. However, people still wanted more and asked for support of the @Else tag. I recently discvovered that Dynamicweb 8.2 now supports this. The template engine now also enables you to use && (and)and || (or) in your expressions. These new features have been announced and documented as silently as the initial support for the @If tags, so I decided to write a little blog post to make you aware of these new possibilities and show you they work.

To better understand why this is useful, consider this example from the original article that outputs different content depending on the web site the request is made to:

<!--@If(Global:Area.ID==1)-->
  Area ID is 1
<!--@EndIf-->
<!--@If(Global:Area.ID!=1)-->
  Area ID is NOT 1
<!--@EndIf-->      

With this code you can present some code when the area ID equals 1 (on my own website, that means the English version of the site) and display other content when the ID is not 1 (which, in my case, is only the Dutch version of the site with an ID of 2).

Although this works well, it's difficult to maintain as the code repeats most of the If tag for each condition. With the @Else tag in Dynamicweb 8.2 this is now much simpler:

<!--@If(Global:Area.ID==1)-->
  Area ID is 1
<!--@Else-->
  Area ID is NOT 1
<!--@EndIf-->      

In addition to the @Else tag, you can now also combine multiple expressions using && (and) and || (or). Imagine you have three web sites in your solution: a US English, a UK English and a Dutch version. Assuming the first two sites have ID 1 and 2, you can display a text in English or in Dutch using this code:

<!--@If(Global:Area.ID==1 || Global:Area.ID==2)-->
  Area ID is 1 or 2; display English text
<!--@Else-->
  Area ID is NOT 1 or 2; display Dutch text
<!--@EndIf-->

(Note: this serves just as an example; you're much better off using the built-in language features to display translated content).

Likewise, you can use AND so multiple conditions must be true for the If block to be executed:

<!--@If(Global:Extranet.UserName != User1 && Global:Extranet.UserName != User2)-->
  User is not User1 or User 2
<!--@Else-->
  User is User1 or User2
<!--@EndIf-->

Unfortunately, it seems that at this stage, nesting Else statements isn't supported. The following code would output a text depending on the average rate value for a product. When the rating is equal to or below 3, the first If block runs. Inside that block the code checks to see if any rating exists for the product. If that's not the case, the text No rating is displayed. In all other cases, a text representing the average value is shown. However, when you try this code now, you won't see the expected output, and it will likely lock up your IIS process. Be warned!

<!--@If(Ecom:Product.Rating >= 0 && Ecom:Product.Rating <= 3)-->
  <!--@If(Ecom:Product.Rating == 0)-->
    No rating
  <!--@Else-->
    Low rating
  <!--@EndIf-->
<!--@Else-->
  High rating
<!--@EndIf--> 

Hopefully, this gets addressed in a future service release.

Happy Elsing!

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