I'm want MT execute a code block a variable number of times and I was hoping <MT:For> would help. The block in question looks like
<mt:For var="iterations" from="0" to="15" step="1"><mt:If (some test)>
CODE BLOCK 1
<mt:Unless name="iterations" eq="0">
<mt:Var name="iterations" op="-" value="1" setvar="iterations">
</mt:Unless><mt:Else>
CODE BLOCK 2</mt:If>
</mt:For>
Even though "iterations" appropriately increments/decrements, the FOR loop only runs 16 times. I've also tried setting "__index__" and "__counter__" per these instructions to no avail.
Any pointers would be greatly appreciated.
[Redacting this response. I think I see what you're trying to do, but also don't think it's possible via quite this method right now.]
Su,
Thanks both for your first response and your clarification. I figured this might be the case.
There's a trick we use for a slightly different situation that I think you can apply here, but I want to check on a couple of things first. I'll update tomorrow.
Su,
My apologies for troubling you. Just wondering if you have more information on the potential fix.
Sorry I blanked this. Had some client stuff suddenly come up.
The basic idea behind the tricks to use when the core operators aren't enough is that you sometimes have to either hack in your own counters, or intentionally overshoot the actual number of things that you want and use tests inside the loop to limit the output; sometimes both.
In your case, just to make sure I'm reading correctly, you're looping over some collection of stuff and always want to output something, but require 16 of whatever satisfies the first condition, correct? That would look something like:
<$mt:var name="mycounter" value="0"$> <mt:For var="iterations" from="0" to="50" step="1"> <mt:unless name="mycounter" eq="16"> <mt:If (conditionA)> [do something] <$mt:Var name="mycounter" op="++"$> <$mt:templatenote note="...and increase our custom counter"$> <mt:Else> [do something else] <$mt:templatenote note="...but leave the counter alone"$> </mt:If> </mt:unless> </mt:For>Disclaimer: I'm fairly sure of this, but haven't actually tested it yet. Either way, if you managed to figure out the For tag, this should illustrate the concept enough for you to tweak if needed.
That "50" is something you'll have to determine yourself. Since we don't actually know exactly how many times to go around the loop, you need to consider your content and come up with a number that is going to be just a bit more than you're likely to ever need. Between two and three times the desired output is probably more than enough, and you can always trim it down.
Then, inside that loop, we use our own counter variable to determine the flow. Once we reach the goal of satisfying conditionA 16 times(and however many of Else), any subsequent loopings up to 50 just get short-circuited by the mt:Unless test. (This is more efficient than it looks.)
Su,
Thank you for getting back to me with such a clever solution. It's so good it somehow feels wrong.
On that note, the increment for mycounter doesn't seem to work. I've emended the code below, adding an HTML style comment to indicate the change.
Thanks for this.
<$mt:var name="mycounter" value="0"$> <mt:For var="iterations" from="0" to="50" step="1"> <mt:unless name="mycounter" eq="16"> <mt:If (conditionA)> [do something] <!-- THE NEXT LINE IS A MODIFICATION //--> <$mt:Var name="mycounter" op="+" value="1" setvar="OutputEntries"$> <$mt:templatenote note="...and increase our custom counter"$> <mt:Else> [do something else] <$mt:templatenote note="...but leave the counter alone"$> </mt:If> </mt:unless> </mt:For>Good catch, and you're right. There's a quirk in the way "op" affects mt:Var I always forget that I personally consider a bug but it seems it's too late to do anything about. The increment operation itself does work, but would require the setvar you added in your rework.