3

How would you perform a conditional on a custom checkbox field in which the user can select 0 -4 different options?

In other words, I have a "Checkboxes" fieldtype named "{side_content_selectables}".

This fieldtype has the following options:

  • Show Search
  • Show Feed Subscription
  • Show Categories
  • Show Recent Posts

Which would just allow them to turn widgets on or off. In my template, I have the following:

<aside id="sidebar">
    {exp:channel:entries channel="side_content" limit="1" dynamic="no"}
        {if {side_content_selectables}"{item}" == "Show Search"{/side_content_selectables}}
            <section class="widget">
                <h3>Search</h3>
                {!-- Search Form --}
            </section>
            <!-- /.widget -->
        {/if}

OR

        {if "Show Feed Subscription"}
            <section class="widget">
                <h3>Subscribe</h3>
                {!-- Feedburner Widget --}
            </section>
            <!-- /.widget -->
        {/if}

Neither of these seem to work though. This makes me feel like a big noob, but I am stumped on this one. ;)

It's almost like I would need to write out a PHP script to perform a contains, but that just seems like overkill and I would like to keep PHP from being used in templates.

5 Answers 5

6

You're best off using the PT Checkboxes, the native ones are just too fragile for real world use.

http://devot-ee.com/add-ons/pt-field-pack

Then:

{if {side_content_selectables:selected option="show_feed"}}
  <p>show the feed</p>
{/if}
2
  • exactly what I was going to suggest.
    – CreateSean
    Dec 10, 2012 at 18:22
  • Thanks James. I usually include those and use them as my default fieldtypes instead of the natives, I just thought I would give it a go on this one. Now I know why I default to P&T. ;)
    – W3bGuy
    Dec 10, 2012 at 18:35
2

I second the answer posted by @James, PT Checkboxes are more flexible, but if you want to use the native checkboxes the following will work:

{checkboxes_field}
    {if item == "Option One"}<p>Option one is checked</p>{/if}
    {if item == "Option Two"}<p>Option two is checked</p>{/if}
    {if item == "Option Three"}<p>Option three is checked</p>{/if}
{/checkboxes_field}

Just replace the custom field name and option values to match your situation.

0

I guess this would be the proper syntax

{if '{side_content_selectables}{item}{/side_content_selectables}' == 'Show Search'}

1
  • 1
    that would only work reliably if one and only one option were selected... Dec 10, 2012 at 18:18
0

If you're stuck with the native field type and can't switch for whatever reason (legacy data etc) then you can always use the phpstringfun plug-in (http://devot-ee.com/add-ons/phpstringfun) and do something along the lines of:

{if "{exp:phpstringfun function='substr_count' par1='Foo'}{checkboxes_field}{/exp:phpstringfun}"=="1"}
Foo Found
{/if}

Hope that helps. :)

0

Just joined, so I can't comment on the accepted answer, but...

the correct syntax should be (note the single quotes):

{if '{side_content_selectables:selected option="show_feed"}'}
    <p>show the feed</p>
{/if}

Maybe there's been a change in the plugin since this was originally posted, but I discovered that without those single quotes, EE will throw a PHP error if none of the checkboxes are checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.