Forum for discussing general topics related to Couch.
8 posts Page 1 of 1
Hi,

how can I use a
Code: Select all
<cms:if>
with a checkbox?
I tried the solution from here: viewtopic.php?f=4&t=11219 but it doesn't work.

I have a checkbox to show or hide content.
Code: Select all
 <cms:editable name="show_book_de" label="Zeige Buch" desc="Zeigt das Buch auf der deutschen Webseite an wenn checkbox aktiv." opt_values='anzeigen' opt_selected = 'anzeigen' type='checkbox' />


and

Code: Select all
        <cms:if k_page_id != ignore_id>

            <cms:set arr_selected='[]' is_json='1' scope='global' />
            <cms:each show_book_de as='opt'>
                <cms:set arr_selected. = opt scope='global' />
            </cms:each>

            <cms:if "<cms:arr_val_exists 'anzeigen' in=arr_selected />">
              <div class="books_item aspect-square">
                <a href="<cms:show k_page_link />">
                  <img class="w-full opacity-75 hover:opacity-100" src="<cms:show image/>"
                      alt="<cms:show k_page_title />"
                      title="<cms:show k_page_title />"
                      onMouseOver="this.src='<cms:show image_hover/>'"
                      onMouseOut="this.src='<cms:show image/>'"
                  >
                </a>
              </div>
            </cms:if>

        </cms:if>

Cheers
Hi,

One quick question -
from the code you posted, I see that the checkbox only has a single value (i.e. only a single checkbox gets shown).
Is that the actual code or have you abridged it for quoting in the post?

I ask this because it is only when a checkbox has multiple values, that we need to loop through all of them to find which are selected (as done in the solution you mentioned).

For a single checkbox, the code would be much simpler.

Please let me know.
Hi,
the checkbox have only one value.
I tried with a normal field if
Code: Select all
<cms:if show_book_de>
but this havn't work.
And i thought the version with the itteration should also work with only one value in the array.
Thanks for the confirmation.

A problem with having a single valued checkbox is that the "opt_selected" parameter cannot be used - because if the user manually unchecks the box, that parameter will check it right back (because it is the default, right?).

It can be solved by using the following amended code (no 'opt_selected' used) -
Code: Select all
<cms:editable name='show_book_de' label='Show Book?' desc='Shows the book on the German website if the checkbox is active' type='checkbox' opt_values='Yes=yes' />

Shows up as -
x.png
x.png (7.29 KiB) Viewed 1534 times

So the default value now becomes unchecked - the user will have to explicitly check if she wishes to select this value.
In the front-end, the code simply becomes -
Code: Select all
<cms:if show_book_de >
    Show book
<cms:else />
    Don't show book
</cms:if>

In case you want the default value to be checked, then use two values with the checkbox.

Hope this helps.
KDJFS wrote: and

Code: Select all
... skipped
            <cms:if "<cms:arr_val_exists 'anzeigen' in=__show_book_de />">
... skipped
            </cms:if>



Cheers


Above code would work with array __show_book_de, as displayed. Couch sets up checkboxes in context as arrays, prepended with double-underscore. This is valid with even single-value checkboxes.

Code that puts checkbox values into array arr_selected becomes redundant and can be removed.
Thanks @trendoman - this indeed is the method to use now (definitely for multi-values; though it will work for a single value too, as you mentioned).
The 'problem' of using 'opt_selected' with a single value will still remain though.
Hi,
thanks for your help!
A togglefield, also a checkbox with two states exist not in couch, or?
KDJFS wrote: Hi,


Checked or not checked, it does not save automatically. You have to re-visit each page in admin-panel and click 'Save'; only then you can use the field's value in frontend. A simple script with db_persist tag can re-save all cloned pages quickly though.
8 posts Page 1 of 1