1

I want to let my customer decide if they want a 2 or 3 column page so I made a radio button but now I need to show different content if they select one of them but I cant get the syntax right.

{% if entry.column.contains ('two') %}
   content here for two columns
{% endif %}

{% if entry.column.contains ('three') %}
   content here for three columns
{% endif %}
2

Close! You can get the selected radio button by its label (what the user sees) or the value you setup in the admin. I'm assuming you're using the value.

{% if entry.column == "two" %}
    {# show 2 column content #}
    2 columns
{% elseif entry.column == "three" %}
    {# show 3 column content #}
    3 columns
{%endif%}

You could also try a switch statement. That gives you a way to fall back to a default option, which might be useful, and a cleaner way to check for more than a couple options (say the client wants a 3 column option with some a special sidebar that doesn't really fit):

{% switch entry.column %}

    {% case "two" %}

       {# show 2 column content #}
       {% include "layouts/_2columns.html" %}

    {% case "three" %}

       {# show 3 column content #}
       {% include "layouts/_3columns.html" %}

    {% case "threespecialsidebar" %}

       {# show 3 column content #}
       {% include "layouts/_3column_sidebar.html" %}

   {% case "default" %}

      {# default to a 1 column layout #}
      {% include "layouts/_default.html" %}

{% endswitch %}

To keep yourself sane, in the switch example, also consider using a include which will bring in a corresponding layout file, depending on which layout you want. (A stray div for example will drive you crazy.... consider putting your markup in separate files.)

One last thing: from a client standpoint, depending on what you're doing, an entry type might be a better way to handle multiple layouts or if there needs to be additional options/fields in a certain layout. (In that case, use entry.type instead of entry.column but the rest of the syntax posted above would be exactly the same).

2
  • Thank you this worked had the feeling I was close but ended up just going try and error coding and still ended up with nothing Thank you so much! Jul 6 '15 at 14:31
  • +1 for entry types. It's better to just specify fields to enter content without things like "1/2/3 columns" options. Layouts can change in the future while content usually stays the same. In other words: don't dictate layout in the CP.
    – user155
    Jul 6 '15 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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