0

I've only been using StringTemplate 4 for a week, so it's probably something I'm doing, but I don't seem to be able to make the <if> work.

I'm using 4.02 (since that's the latest in Maven repository). I have a class called Variable. This is a snippet:

class Variable
{
  ...
  public boolean isArray()
  {
    return _bIsArray;
  }
}

I have a template that has a line(delimiter is $, $):

$if(x.isArray)$ $ArrayAdd(x, className)$ $endif$

If I remove the if and simply let it execute $ArrayAdd(...)$ for everything, the ArrayAdd is clearly executed. I then put the $if$ back in. I also put a print statement in isArray() and isArray() is getting executed and returns false most of the time, but does return true once in a while (for exactly the cases I expected). However, $ArrayAdd never gets executed from within the $if$.

I looked at the trace (which I'm not good at reading) and got:

declareSetGet:0227: load_local    0         stack=[ ], calls=ObjectClass _sub1  
declareSetGet, sp=-1, nw=0
declareSetGet:0230: load_prop     #25:"isArray" stack=[  
altLocation<CUSTOM>::Array<1>::Custom<altLocationObj> ], calls=ObjectClass _sub1 
declareSetGet, sp=0, nw=0
declareSetGet:0233: brf           254       stack=[ null ], calls=ObjectClass _sub1 
declareSetGet, sp=0, nw=0
ObjectClass:0121:   newline                 stack=[ ], calls=ObjectClass, sp=-1, nw=959
ObjectClass:0122:   write_str     #15:"}"   stack=[ ], calls=ObjectClass, sp=-1, nw=0

This is one of the cases where I would expect the ArrayAdd template to be executed. Obviously, it doesn't.

Can anyone tell me what I'm missing?

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280

1 Answers1

0

I'm wondering if you should do this:

$if(x.array)$ $ArrayAdd(x, className)$ $endif$

Specifically, use x.array instead of x.isArray because the name of the property is "array" and the "is" is just a prefix per the Java Beans convention for boolean property accessors.

Jack Leow
  • 21,945
  • 4
  • 50
  • 55
  • I would have bet that wouldn't make a difference. I would have lost :) It works correctly now! I still don't understand why it was calling the routine but apparently not using the result (where you're version does). But, I'll move on. Thanks for the help. – user2615860 Jul 24 '13 at 21:44