Include code in multiple transformations

Alan Isaacson asked on November 14, 2016 14:19

I have an If statement that I have included in a single transformation but would ideally like to include in about 30 others, is it possible to call in the logic in question to multiple transformation?

eg: My transformation sets a variable based on which part of the website you are in.

if (CurrentDocument.Parent.Parent == "Canis"){Prefix = "Canis";}
if (CurrentDocument.Parent.Parent == "Felis"){Prefix = "Felis";}
if (CurrentDocument.Parent.Parent == "Lapis"){Prefix = "Lapis";}
if (CurrentDocument.Parent.Parent == "Equis"){Prefix = "Equis";}
if (CurrentDocument.Parent.Parent == "Exotis"){Prefix = "Exotis";}
if (CurrentDocument.Parent.Parent == "Bovis"){Prefix = "Bovis";}

I would like to call the variable Prefix into many transformation such as

"blahblah" + Prefix

Correct Answer

Jim Spillane answered on November 14, 2016 18:58

Sorry about that. Adding a return; to the Includes_SetPrefix transformation should clear it up:

{% 
Prefix = CurrentDocument.DocumentName;
return; 
%}

You can then use the variable Prefix in different ways:

{% 
// get the Includes_SetPrefix transformation code
Transformations["CMS.Root.Includes_SetPrefix"].TransformationCode;
#%}

{% return "blahblah" + Prefix; %}

{% Prefix = Prefix + "blahblah"; return; %}

{% Prefix %}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on November 14, 2016 14:38

1 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on November 14, 2016 15:39 (last edited on November 14, 2016 15:47)

When we met in Vegas, I told you I would ask a lot of questions with simple answers. Not overly familiar with C# and Visual studio, is there an easy way to code the below into the example in the docs?

if (CurrentDocument.Parent.Parent == "Canis"){Prefix = "Canis";}
if (CurrentDocument.Parent.Parent == "Felis"){Prefix = "Felis";}
if (CurrentDocument.Parent.Parent == "Lapis"){Prefix = "Lapis";}
if (CurrentDocument.Parent.Parent == "Equis"){Prefix = "Equis";}
if (CurrentDocument.Parent.Parent == "Exotis"){Prefix = "Exotis";}
if (CurrentDocument.Parent.Parent == "Bovis"){Prefix = "Bovis";}
0 votesVote for this answer Mark as a Correct answer

Jim Spillane answered on November 14, 2016 15:54

Hi Alan -

You can try creating a Text/XML transformation like the following:

Image Text

Then in the transformations that you want to include that code you can use the following:

{% 
// get the Includes_SetPrefix transformation code
Transformations["CMS.Root.Includes_SetPrefix"].TransformationCode

// print the Prefix variable
println(Prefix);

#%}
0 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on November 14, 2016 18:23

Interestingly I cannot get the variable to write out to the page with this method. If I manually set a variable in the page and use println(Prefix); it works fine

0 votesVote for this answer Mark as a Correct answer

Alan Isaacson answered on November 15, 2016 11:19

Many thanks, that will save a lot of time.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 21, 2016 19:47

I don't use text/xml transformations but this is pretty cool Jim!

0 votesVote for this answer Mark as a Correct answer

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