Looping forms

KiF

Active member
Looping forms always output ($m - 1) forms.

PHP:
<xf:set var="$m" value="1" />
<xf:foreach loop="range(0, $m)" value="$value">
    <li><form></form></li>
</xf:foreach>
 
That exact code works for me as I'd expect.

What output are you getting, and what output are you expecting?
 
I'm expecting to view <form id="f-1"></form> inside <li id="l-1"></li> in xfrm_resource_edit template.

I paste the following after <xf:macro template="xfrm_resource_edit_macros" name="tag_line" arg-resource="{$resource}" />:
HTML:
<xf:set var="$m" value="1" />
<xf:foreach loop="range(0, $m)" value="$value" i="$i">
    <dl class="formRow">
        <dt>
            <div class="formRow-labelWrapper">
                <label class="formRow-label"></label>
            </div>
        </dt>
        <dd>
            <ul>
                <li id="l-{$i}">
                    <form id="f-{$i}"></form>
                </li>           
            </ul>
        </dd>
    </dl>
</xf:foreach>

And get this
HTML:
<dl class="formRow">
    <dt>
        <div class="formRow-labelWrapper">
            <label class="formRow-label"></label>
        </div>
    </dt>
    <dd>
        <ul>
                <li id="l-1"></li>
        </ul>
    </dd>
</dl>
<dl class="formRow">
    <dt>
        <div class="formRow-labelWrapper">
            <label class="formRow-label"></label>
        </div>
    </dt>
    <dd>
        <ul>
                <li id="l-2">
                    <form id="f-2"></form>
                </li>
        </ul>
    </dd>
</dl>
 
To break it down, range(0, 1) creates an array which contains two values: 0 and 1. So if you loop over it, you're going to do so twice. The first time, $value will be 0 and the second time, $value will be 1.

$i is a value which counts how many iterations there has been. So the first iteration it will have a value of 1, and the second iteration it will have a value of 2.

With all of that in mind, the output you're getting is expected.
 
Sorry, but I don't understand why there is no output inside <li id="l-1"></li> the form, but get it inside <li id="l-2">.

Expecting this (in red) but get nothing:
Rich (BB code):
<dl class="formRow">
    <dt>
        <div class="formRow-labelWrapper">
            <label class="formRow-label"></label>
        </div>
    </dt>
    <dd>
        <ul>
                <li id="l-1">
                    <form id="f-1"></form>
                </li>
        </ul>
    </dd>
</dl>
<dl class="formRow">
    <dt>
        <div class="formRow-labelWrapper">
            <label class="formRow-label"></label>
        </div>
    </dt>
    <dd>
        <ul>
                <li id="l-2">
                    <form id="f-2"></form>
                </li>
        </ul>
    </dd>
</dl>
 
Your code is working as expected for me.

HTML:
<xf:set var="$m" value="1" />
<xf:foreach loop="range(0, $m)" value="$value" i="$i">
    <dl class="formRow">
        <dt>
            <div class="formRow-labelWrapper">
                <label class="formRow-label"></label>
            </div>
        </dt>
        <dd>
            <ul>
                <li id="l-{$i}">
                    <form id="f-{$i}"></form>
                </li>          
            </ul>
        </dd>
    </dl>
</xf:foreach>

Selection_440.webp
 
@Bob is it ok in xfrm_resource_edit template?
Depends on where you put it. If you are trying to put it inside the existing FORM element on that template, that would be considered "Form Nesting" which I doubt will work. If you put it outside of the existing form element, Im sure it will work just fine as you can have multiple form elements on a single document as long as they are not nested. Probably best to have @Chris D or @Mike or @Kier confirm this tho.
 
Certainly if you’re nesting forms, that could be the explanation as that’s not something I did in my testing.
 
Back
Top Bottom