Nested if statements in XML transformation

Liz B asked on January 3, 2019 19:27

I created a custom page type with text/xml transformation and it works well, but I created a new field with a boolean data type for adding functionality to a button to make it open in a new tab. The idea is if it gets checked than the link for a button should open in a new tab. I want to use a conditional like IfTrue to check if the boolean was checked and add a "target='_blank'" to my anchor tag. I'm running into issues where none of the if statements I use work. I've tried different conditional variations with nothing working. My html is within its own if statement that can't change because that checks if a feature is being used. The code I'm having trouble with is the IfTrue((ButtonUrlTarget)). Everything else works. This is my code:

{% if((UseFeature.ToLower()) == "yes") { return "<section>"+ IfEmpty((ButtonUrl), "", "<a href='"+ButtonUrl+"' '"IfTrue((ButtonUrlTarget), "", "target='_blank'")"' class='button'>"+ButtonName+"</a>") +"</section>" }

Correct Answer

Brenden Kehren answered on January 3, 2019 19:55

I'd update your syntax to use something like this:

{% if((UseFeature.ToLower()) == "yes") { %}
    <section>
        {% if(ButtonURL != "") { "<a href='" + ButtonURL + "'" + if(ButtonURLTarget) { " target='_blank'" } else { "" } + ">" + ButtonName + "</a>" } else { "" }
    </section>
{% } %}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Liz B answered on January 3, 2019 20:59

Thank you! I had to modify it a little, but it worked!

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 3, 2019 21:27

IfTrue - is method for ASCX transformation type, for text xml tranformaiton type you should use macro expressions:

{%
result = "";
if (!String.IsNullOrEmpty(ButtonUrl)) 
{ 
    target = String.IsNullOrEmpty(ButtonUrlTarget) ? "" : " target='_blank' ";
    result  = "<a href='" + ButtonUrl + "' " + target  + " class='button'>" + ButtonName + "</a>";

}
result = "<section>" + result + "</section>";
if (UseFeature.ToLower() == "yes") { return result; }
#%}
0 votesVote for this answer Mark as a Correct answer

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