Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
Hi, I have a particular options set to a template page and have the following code

Code: Select all
<cms:pages masterpage='image-settings.php'>
        <cms:if image_01>
                <div class="image">
                        <cms:show image_01 />
                </div>
        </cms:if>
        <cms:if image_02>
                <div class="image">
                        <cms:show image_02 />
                </div>
        </cms:if>
        <cms:if image_03>
                <div class="image">
                        <cms:show image_03 />
                </div>
        </cms:if>
</cms:pages>


But instead, is there a way to retrieve them as an array, so I can take advantage of using PHP's foreach() loop to run something like:

Code: Select all
<cms:pages masterpage='image-settings.php'>
        <cms:foreach images as image>
                <cms:if image>
                        <div class="image">
                                <cms:show image />
                        </div>
                </cms:if>
        </cmd:endfor>
</cms:pages>


So I don't have to repeat the code?

Thank you!
I am afraid but we don't have arrays in Couch.
The closest we can get to what you specified is the following
Code: Select all
<cms:each 'image_01 | image_02 | image_03' as='image' >
    <cms:if "<cms:get image />">
        <div class="image">
            <cms:get image />
        </div>
    </cms:if>
</cms:each>

The cms:each takes a '|' separated string of values and internally converts that to an array.
Please also note that we are using cms:get tag (and not the usual cms:show tag) to get the values (please see http://www.couchcms.com/docs/tags-reference/get.html if not familiar).

Perhaps this would save you a little typing :)
Thanks.
Wow, that was quick! That really helps. Just one more question, if I want to use variable counter like ++$counter in PHP in this loop, should I use

<cms:php>++$counter</cms:php>

or is there another way using CouchCMS tags for variable operations?
Not sure how exactly you wish to use the PHP statement but, yes, we can always intersperse PHP with Couch script.

Couch natively also has some tags that could help with incrementing/decrementing variable values, namely -
* add
* sub
* mul
* div
* mod
* incr
* decr
Please see http://www.couchcms.com/docs/tags-reference/ for their details.
4 posts Page 1 of 1
cron