If-then Statement in Text/XML Transformation Not Working

Andre Pfanz asked on March 15, 2016 19:10

I'm using a repeater that shows people's information, including their image with Text/XML transformation. Some people do not have photos, so I want to hide the image tag for them. I tried this:

{% if (Image != "")  { "<img class=\"img-responsive\" src=\"" + Image  + "\"/>" } else { } %}

Where Image is a file upload field. The problem is the image tag is always appearing - even for those whose value for Image is blank.

Does anyone know what I'm missing?

Correct Answer

Trevor Fayas answered on March 15, 2016 19:30

I often have to use the 'return' statement in the curly, may also want to use the string.IsNullOrWhiteSpace()

{% if (!string.IsNullOrWhiteSpace(Image)) { return "<img class=\"img-responsive\" src=\"" + Image + "\"/>" } |(user)zbynekh|(hash)ec99f55b038acfc9fd726dc6b8d8ea078e1c55ff0e82d13eca64fd5acdd21dcc%}

try that, may also be able to simplify via

{% (!string.IsNullOrWhiteSpace(Image) ?"<img class=\"img-responsive\" src=\"" + Image + "\"/>" : "") |(user)zbynekh|(hash)1d7830c3cf04ffe66b0449256538f492da8afe7fc7247c96d2d91d760938a770%}

2 votesVote for this answer Unmark Correct answer

Recent Answers


Andre Pfanz answered on March 15, 2016 19:34

Thanks Trevor - that worked!

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on March 16, 2016 12:18

Hi Andre,

Though you have got an answer however, this should also work for you. It's good to have multiple syntax doing same work.

{% IfEmpty(Eval("Image"), <img class=\"img-responsive\" src=\"" + Image + "\"/>" : "") ) %}

Please mark this as a scorrect solution if this works for you.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

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