Setting variable as a string with K# in Transformation

Nathan Trost asked on August 28, 2014 23:17

I'm very new to Kentico and C# for that matter, but have been all over here and the internet in general and been unable to figure out how to do something so simple.

I'd like to conditionally set a variable to a string to check against in my markup. I'm able to set the variable, but every method I use to do so ends up outputting the String when it's declared as well. I've tried using the standard C# "String" syntax when I declare as well as many other variations and nothing seems to work. How can I sanitize the string value in my macro to only show up when I call the variable?

{% if(DisplayType == 0){ spClass = EmbeddedWrapperClass; altPos = "right"; }

%}

Correct Answer

Jim Spillane answered on August 29, 2014 05:02

in K# I add a return; to keep the string from being outputted.

{% 
  if (DisplayType == 0) {
    spClass = EmbeddedWrapperClass;
    altPos = "right";
    return;
  }
|(identity)GlobalAdministrator%}
2 votesVote for this answer Unmark Correct answer

Recent Answers


Delford Chaffin answered on August 29, 2014 03:29 (last edited on August 29, 2014 03:29)

I ran across a similar need a while back. It is more cumbersome than you'd imagine, but this works:

http://devnet.kentico.com/questions/variables-in-transformations

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 29, 2014 03:30

In my opinion, I think the best thing to do is create a custom transformation method in code behind which accepts your parameter, much cleaner, reusable approach.

0 votesVote for this answer Mark as a Correct answer

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