Macro to change path based on Page Type

Duncan Koza asked on September 3, 2019 19:32

I am trying to write a macro for the path field in a repeater. I have tested individual pieces of this code seperatly but I am wondering if I have a syntax error because I can't get this to work. Any buddy know if this is the proper syntax for an if statement?

{% if (CurrentDocument.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.ClassName == "custom.sjcg_MainPage") { DocumentContext.CurrentAliasPath };

else (CurrentDocument.Parent.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.Parent.ClassName == "custom.sjcg_MainPage") { Parent.NodeAliasPath };

else (CurrentDocument.Parent.Parent.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.Parent.Parent.ClassName == "custom.sjcg_MainPage") { Parent.Parent.NodeAliasPath };

%}

Correct Answer

Brenden Kehren answered on September 3, 2019 19:55

Kentico K# Macros only allow a simple if() { } else { } syntax. You cannot do elseif statements.

It looks like your syntax, when properly parsed out, should look like this:

{% if (CurrentDocument.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.ClassName == "custom.sjcg_MainPage") { DocumentContext.CurrentAliasPath } else { if(CurrentDocument.Parent.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.Parent.ClassName == "custom.sjcg_MainPage") { CurrentDocument.Parent.NodeAliasPath } else { if(CurrentDocument.Parent.Parent.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.Parent.Parent.ClassName == "custom.sjcg_MainPage") { Parent.Parent.NodeAliasPath } else { CurrentDocument.NodeAliasPath } } } |(identity)GlobalAdministrator%}

I've not tested this and it's pretty messy but should get you going in the right direction.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Duncan Koza answered on September 3, 2019 20:20 (last edited on December 10, 2019 02:31)

Thank you so much. I struggle with the syntax.

I have used your code and found out that both examples below work:


{% if (CurrentDocument.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.ClassName == "custom.sjcg_MainPage") {DocumentContext.CurrentAliasPath} else { if (CurrentDocument.Parent.ClassName == "custom.sjcg_SectionPage" || CurrentDocument.Parent.ClassName == "custom.sjcg_MainPage") {Parent.NodeAliasPath} else {Parent.Parent.NodeAliasPath} } |(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on September 4, 2019 09:06 (last edited on December 10, 2019 02:31)

I would recommend to simplify and optimize the macro a bit to look better. Something like:

{% x = CurrentDocument.ClassName; y = CurrentDocument.Parent.ClassName; z = CurrentDocument.Parent.Parent.ClassName if (x == "custom.sjcg_SectionPage" || x == "custom.sjcg_MainPage") { DocumentContext.CurrentAliasPath } else { if(y == "custom.sjcg_SectionPage" || y == "custom.sjcg_MainPage") { CurrentDocument.Parent.NodeAliasPath } else { if(z == "custom.sjcg_SectionPage" || z == "custom.sjcg_MainPage") { Parent.Parent.NodeAliasPath } else { CurrentDocument.NodeAliasPath } } } |(identity)GlobalAdministrator%} and it will return desired value - it is better for maintenance, performance and easier to use.

0 votesVote for this answer Mark as a Correct answer

Duncan Koza answered on September 4, 2019 18:23 (last edited on December 10, 2019 02:31)

Thank you Juraj Ondrus,

This is very help. I am going to look into the custom macro link you provided. Now that I am starting to learn more about syntax, I was able to shorten the one you gave me. Thank you.

{% x = CurrentDocument.ClassName; y = CurrentDocument.Parent.ClassName; z = CurrentDocument.Parent.Parent.ClassName; if (x == "custom.sjcg_SectionPage" || x == "custom.sjcg_MainPage") { CurrentDocument.NodeAliasPath } else { if(y == "custom.sjcg_SectionPage" || y == "custom.sjcg_MainPage") { CurrentDocument.Parent.NodeAliasPath } else { CurrentDocument.Parent.Parent.NodeAliasPath } } |(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

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