How to write if else condition in transformation

raj k asked on November 19, 2014 10:30

How to write if else condition in transformation

Correct Answer

Jan Hermann answered on November 19, 2014 14:47

It depends on the transformation type. For most of them (like Text/XML) you can simply use this one:

{%
if (...) {
...
}
else {
...
}
|(identity)GlobalAdministrator%}

However, if you use ASCX transformations, you need to use IfCompare method:

<%# IfCompare(1, 2, "The values are different", "The values are equal") %>

Best regards,
Jan Hermann

0 votesVote for this answer Unmark Correct answer

Recent Answers


Vilém Jeniš answered on November 20, 2014 16:19 (last edited on December 10, 2019 02:30)

I would also suggest this forum thread: http://devnet.kentico.com/forums?forumid=65&threadid=40763.

I find the idea of writing placeholders with visibility conditions quite heplful and used it myself. It's a great way of mimicking the function of this:

{%
if (...) {
    ...
}
else {
    ...
}
|(identity)GlobalAdministrator%}

when you really need ASCX transformations.

In short, this approach can be useful, when conditions in transformations get complicated:

<asp:Placeholder runat="server" ID="pl1" visible='<%# ... %>'> 
    ...
</asp:Placeholder>
<asp:Placeholder runat="server" ID="pl2" visible='<%# !pl1.Visible %>'> 
    ...
</asp:Placeholder>
3 votesVote for this answer Mark as a Correct answer

Arindam Debnath answered on June 14, 2018 11:46 (last edited on December 10, 2019 02:31)

Adding my two pennies worth. If someone has issues text/xml transformation with the code blocks listed by Jan and Vilem, you may try enclosing each conditional line within macro wrapper as shown below.

{% if (Documents[NodeAliasPath].Children.Count == 0) { %}

Some stuff

{% } else { %}

Some other stuff

{% } |(identity)GlobalAdministrator%}

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.