Conditional Transformation?

Tom F asked on January 6, 2017 00:17

Hi I have a hierarchical transformation and in one of the ascx transformations I'd like to conditionally apply a transformation to the current element depending on a property in the parent.

Ideally I'd like to do something like ApplyTransformation to the given datarow in the ascx macro.

In pseudo code:

if(parent.booleanValueIsSet) {
//render transformation 1
} else {
//render transformation2
}

I was wondering how to make the call to render either transformation on the current element. I can't seem to find anything in the api documentation

Correct Answer

Trevor Fayas answered on January 6, 2017 00:37

You have a couple options here.

First if possible (I know you mentioned it's an ASCX hierarchy, but you can make individual transformations either text/xml or ascx, you can mix and match) is to utilize K# (text/xml transformation type), in it you can use the following macro to transform:

DataItem.ApplyTransformation((parent.booleanValueIsSet ? "Custom.MyTransformationName1" : "Custom.MyTransformationName2"), "ContentBefore", "ContentAfter")

If you MUST use ASCX, then you can either reference the CMS.MacroEngine.MacroContext.CurrentResolver.ResolveMacros() method (passing it the same k# macro above, although not sure if the DataItem will be available, it may), or you may need to put a Repeater control in your ASCX transformation, then use a server script to dynamically set it.

 <cms:repeate runat="server" id="MyRepeater"/>
 <script runat="serve">
    public override OnInit() {
        MyRepeater.Path = Eval<string>("NodeAliasPath");
        MyRepeater.TransformationName = (parent.booleanValueIsSet ? "Custom.MyTransformationName1" : "Custom.MyTransformationName2");
    }
    </script>

I would get the exact code, but right now i'm VPN-ed out of my wok computer with the sample, so if you need me to post it just tell me, otherwise i would go with the macro method!

1 votesVote for this answer Unmark Correct answer

Recent Answers


Tom F answered on January 6, 2017 01:03

Hi Trevor thanks what an awesome answer. I'll mix and match and see if it gets me there.

Thanks very much

0 votesVote for this answer Mark as a Correct answer

Tom F answered on January 6, 2017 01:29 (last edited on December 10, 2019 02:30)

In a transformation I tried

{% Object.DocumentName |(identity)GlobalAdministrator%}

The document name is spit out to screen but the output of applytransformation is blank.

I've checked the transformation name it is correct..

I realised I can't mix match ascx because apply transformation only works with text/html transformation types so I'll convert them over.. there's no real need for the ascx other than I prefer the syntax

Thanks for your help :)

0 votesVote for this answer Mark as a Correct answer

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