Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Greetings.

I was wondering how it will be possible to integrate a <cms:if> statement with an editable region with multiple potential values?

For example, say in my <cms:template> i have
Code: Select all
<cms:editable name='options' label='Options' desc='Select some options' type='checkbox' opt_values='apple | banana | orange | pear | grape'></cms:editable>

What sort of if statement would I use to determine which checkboxes are selected? I want to display a particular block of code depending on which checkbox or boxes are selected. Here's a sort of idea of what I want here (code not functional):
Code: Select all
<cms:if options='apple'>
  <p>You picked an apple</p>
</cms:if>
<cms:if options='banana'>
  <p>You picked a banana </p>
</cms:if>
<cms:if options='orange'>
  <p>You picked an orange </p>
</cms:if>
<cms:if options='pear'>
  <p>You picked a pear </p>
</cms:if>
<cms:if options='grape'>
  <p>You picked a grape</p>
</cms:if>


This works fine if the user only selects one option, but I need to also handle if the user selects multiple options, unfortunately it just returns separated values.
How can I get this to work?
TIA!
Hi,

To iterate through the values contained in a checkbox, we need to use the <cms:each> tag (http://docs.couchcms.com/tags-reference/each.html).

For example, please try the following -
Code: Select all
<cms:each options as='opt'>
    <cms:if opt='apple'>
        <p>You picked an apple</p>
    </cms:if>
    <cms:if opt='banana'>
        <p>You picked a banana </p>
    </cms:if>
</cms:each>

Hope it helps.
Aha thank you! I was a little confused by how to implement that 'each' tag. Thanks!
So I've tried out the each tag. Is it possible to mix the <if> inside the each with an <else>? I'm trying to do it with the above example however it just outputs ALL the possibilities.
So for example

Code: Select all
<cms:each options as='opt'>
    <cms:if opt='apple'>
        <p>You picked an apple</p>
   <cms:else/>
        <p>You didn't pick an apple</p>
    </cms:if>
    <cms:if opt='banana'>
        <p>You picked a banana </p>
   <cms:else/>
        <p>You didn't pick a banana </p>
    </cms:if>
</cms:each>

The above code for me will output something along the lines of
Apple:
You picked an apple
You didn't pick a banana
Banana:
You picked an apple
You didn't pick a banana

However I'm trying to keep the responses isolated, ie. Apple should only display parameters relevant to Apple and not things applicable to banana.

Please let me know if I should rather post the actual code that I am working on as it is a slightly different (same concept, just a few extra div's etc inside, perhaps I'm overlooking something).

TIA!
Please post some sample output corresponding to various selections e.g. apple+banana, apple+orange, only apple etc.
No need to post what code you are trying to use to get that output - just the desired output.
Thanks for the response KK.

What I'm trying to do is let the user pick a selection of checkboxes in the CMS. On the fronted each checkbox corresponds to a DIV which displays a specific value if the checkbox is checked, or a default value if the checkbox is not checked.
For example in the CMS there are checkboxes,
Fruits:
Apple
Banana
Pear
Grape

On the front end there is a block of code for each fruit
Apple Banana Pear Grape
Default Default Default Default

if the checkbox is selected for all fruit, it would display
Apple Banana Pear Grape
Selected Selected Selected Selected

If its just for apple + banana
Apple Banana Pear Grape
Selected Selected Default Default

etc etc.

(The problem i was having is it would draw on my front end (for say just apple + banana):
Apple Banana Pear Grape
Selected Default Default Default
Apple Banana Pear Grape
Default Selected Default Default
Apple Banana Pear Grape
Default Default Default Default
Apple Banana Pear Grape
Default Default Default Default)

TIA!
OK, I get it now.
Please provide me with the actual HTML code you have on the frontend and I'll let you know how to set the values the way you want.
I am having the exact same issue. Any chance you could put the solution here?

Thank you
One way would be to store all selected values into an "array" (viewtopic.php?f=5&t=10892).
For example like this -
Code: Select all
<cms:set arr_selected='[]' is_json='1' scope='global' />
<cms:each options as='opt'>
    <cms:set arr_selected. = opt scope='global' />
</cms:each>

In the code above we begin with an empty global array named arr_selected and then iterate through our checkbox region (named 'options' in our example) stuffing each selected value found into the array.

So, suppose only 'banana' and 'pear' were selected, if we were to print out the resulting array as follows -
Code: Select all
<cms:show arr_selected as_json='1' />

It would show this -
Code: Select all
["banana","pear"]

Ok, now once we have the array, we can query it to see if it contains a particular value e.g. as follows -
Code: Select all
<cms:arr_val_exists 'banana' in=arr_selected />

The above would output '1' (if the value was not present it would be '0').

We can use that to make our decisions as follows -
Code: Select all
<cms:if "<cms:arr_val_exists 'banana' in=arr_selected />">
    Banana selected.
<cms:else />
    Banana not selected.
</cms:if>
<br>

<cms:if "<cms:arr_val_exists 'apple' in=arr_selected />">
    Apple selected.
<cms:else />
    Apple not selected.
</cms:if>

Final code all in one place would be this -
Code: Select all
<cms:set arr_selected='[]' is_json='1' scope='global' />
<cms:each options as='opt'>
    <cms:set arr_selected. = opt scope='global' />
</cms:each>

<cms:if "<cms:arr_val_exists 'banana' in=arr_selected />">
    Banana selected.
<cms:else />
    Banana not selected.
</cms:if>
<br>

<cms:if "<cms:arr_val_exists 'apple' in=arr_selected />">
    Apple selected.
<cms:else />
    Apple not selected.
</cms:if>
<br>

Hope it helps.
Worked perfectly! Thank you very much for the awesome support, as always.
10 posts Page 1 of 1
cron