user-pic

MT:For and altering loop iterations

Vote 0 Votes

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.

7 Replies

| Add a Reply
  • [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.

Add a Reply

If you need to share template code, replace all the "<" signs with "&lt;" or use this utility.

Forum Groups

1773 6162

Last Topic: Excluding categories from blog by kholechek on Feb 9, 2012

86 302

Last Topic: website entries by masoud on Oct 26, 2011

1428 5076

Last Topic: Stupid Question? How to make a static homepage by Jeremy on Feb 3, 2012

694 2908

Last Topic: Blogspot to Movable Type by sdude on Feb 8, 2012

84 291

Last Topic: How to have some other characters in entry basename automatically written by Afshin Haghighatnia on Dec 22, 2011

173 737

Last Topic: About the MT version stated in HTML source by Alex E. Schneider on Feb 7, 2012

190 565

Last Topic: Analytics Reporting by michael webster on Feb 5, 2012

48 210

Last Topic: An idea and also a request by Afshin Haghighatnia on Jun 29, 2011

64 246

Last Topic: jQuery in MT 5.1 still at 1.4 - why? by perlmonkey on May 25, 2011

code.sixapart.com

137 478

Last Topic: Getting a thumbnail with xpath by Peter on Mar 13, 2011

222 716

Last Topic: Custom Field for Asset Not Appearing by android on Feb 9, 2012