Kentico CMS 6.0 Developer's Guide

Variant conditions

Variant conditions

Previous topic Next topic Mail us feedback on this topic!  

Variant conditions

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

When setting up a personalized page, the most important part of the process is to properly define the conditions that indicate when individual variants should be displayed. By utilizing macro expressions, you can write conditions for virtually any type of scenario according to your specific requirements. Keep in mind that the result of a condition's expression must be a logical (boolean) value in order for it to work correctly. For details about available macro options and syntax, please refer to the Development -> Macro expressions chapter.

 

A variant's condition is specified in its Display condition property.

 

devguide_clip1731

 

Through the edit icon (Edit), you can use the Macro condition editor, which provides a graphical interface that makes it easier to build complex conditions.

 

On the live site, each personalized object will only have one of its variants displayed at any given moment. Variants are processed in the order of their priority and the first enabled variant whose condition is fulfilled in the current context will be selected. The original object is displayed in cases where the conditions of all variants are resolved as false.

 

By default, the priority of variants depends on the order in which they were created, as can be seen on the personalization slider. Apart from the original object, which is always first, variants with a higher priority can be found further on the left of the slider.

 

devguide_clip1730

 

If you wish to change the order in which an object's variants should be processed, click the Variant list button among the actions on the slider. A dialog containing a list of the variants will be opened as shown below.

 

devguide_clip1722

 

Here, you can reorganize the variants as necessary through the Up (Up) or Down (Down) actions.

 

Condition example

 

The example below demonstrates how you can build a macro that causes the given variant to be displayed only during a specific part of the day, or to users with special authorization.

 

Create a content personalization variant for an object, open its properties dialog and click the edit icon (Edit) next to the Display condition field to open the macro condition editor. Stay on the Designer tab, remove (Delete) any existing expressions, then click the Add Group (AddWebPart) action and use Add expression (AddMacroExpression) to create two expression fields inside the new group. Fill in the expressions according to the following:

 

Enter CurrentDateTime.Hour into the field on the left, select the >= operator (greater than or equal to) and write 9 on the right.

Enter CurrentDateTime.Hour into the field on the left, select the <= operator (less than or equal to) and write 17 on the right.

 

Leave the AND option selected between the two expressions.

 

Then, add another expression to the main area and select the OR option between it and the group. Enter CurrentUser.IsInRole("GoldPartners") into the expression's field and select (no operation).

 

devguide_clip1709

 

If you click OK, the final result will look like the following:

 

((CurrentDateTime.Hour >= 9) && (CurrentDateTime.Hour <= 17)) || CurrentUser.IsInRole("GoldPartners")

 

This condition would ensure that the variant is only displayed to ordinary users if the current time is between 9 AM and 5 PM. Users who belong to the GoldPartners role will be able to see the content specified by the variant at any time.