macro oddity

Yang Wen asked on August 19, 2016 18:31

I'm trying to write this simple conditional logic in my Text/XML transform. Looking at the logic, it should only be setting a variable to a bool value. However I actually get an output of FALSE in my page. I"ve confirmed that the line below is outputting FALSE. Any idea why?

{% if(DataItemIndex == 0){firstIndex = true;}; if(DataItemIndex > 0){firstIndex = false;}; #%}

Recent Answers


Brenden Kehren answered on August 19, 2016 18:59 (last edited on August 19, 2016 18:59)

You might try to use the macro for checking if it's the first one or not.

{% IsFirst() %} should give you the same without any other overhead.

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on August 19, 2016 22:05

So that method didn't work in my 8.1 instance. I get this error in event lot:

 Method 'IsFirst' not found, please check the macro or the method declaration.

I noticed the documentation officially has IsFirst as a .NET method. It only says Macro equivalent exists for "Most" of them. I wish the documentation denotes which methods are available as a macro.. (argh!)

I think i'm more interested in figuring out how the macro gets interpreted by Kentico. The behavior I observed is not intuitive at all.

0 votesVote for this answer Mark as a Correct answer

Yang Wen answered on August 19, 2016 22:16

Looking at the macro syntax reference guide again. ALthough it doesn't explicitly explain the behavior, it appears that simply setting a variable to a value, implicitly returns that value in the HTML output.

{% x = 5 %} 

The above macro expression prints a 5 to the page. However intuitively you'd think it only sets the variable. Is this behavior mentioned somewhere in the document and I just missed it?

0 votesVote for this answer Mark as a Correct answer

Jim Spillane answered on August 20, 2016 02:35

Take a look at the Conditional statements section.

Use the if command to create conditions in format: if (<condition>) {<expressions>}. The condition statement returns the value of <expressions> if the condition is true, and a null value if false.

You are seeing the null value of the false condition of the if. By adding a return command, you can specify the value of firstIndex to be returned rather than the null value that the if would return if it is false...

{%if(DataItemIndex == 0){firstIndex = true;}; if(DataItemIndex > 0){firstIndex = false;}; return firstIndex; #%}

If you wanted to use the firstIndex value later you can make a structure that 'returns' nothing but sets the value of the variable firstIndex...

{%
if(DataItemIndex == 0)
  {firstIndex = true;}
else
  {firstIndex = false;}
return;
%}

<span>{% println(firstIndex); %}</span>
2 votesVote for this answer Mark as a Correct answer

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