Forum

Thread tagged as: Question, Problem

How to use conditionals with categories in a page template?

Hi,

I'm trying to use the categories field to show html content based on the selected category. Using the docs I have the following code but none of the conditionals seem to be working.

I think that I have the id wrong for categories, can anyone suggest where I'm going wrong please?

    <perch:categories id="tan-type" label="Tan Type" set="tanning-types" order="4" />
    <perch:if id="tan-type" value="light">
      <i class="badge light">&nbsp;</i>
    <perch:elseif id="tan-type" value="medium" />
      <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i>
    <perch:elseif id="tan-type" value="dark" />
      <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i><i class="badge dark">&nbsp;</i>
    <perch:elseif id="tan-type" value="extra-dark" />
      <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i><i class="badge dark">&nbsp;</i><span class="primary-highlight">+</span>
    </perch:if>
Andrew Cetnarskyj

Andrew Cetnarskyj 0 points

  • 5 years ago

In your if conditionals your missing match="eq" which could be why is failing... Though I think "eq" is assumed by default, ... This is an untested obsurvation though.

Is there actually a perch:elseif tag? I know of perch:else tag... I did a quick search in the documentation and did not find any results for elseif.

Drew McLellan

Drew McLellan 2638 points
Perch Support

There is no perch:elseif tag. Even I had to check that one.

I've updated the code to only have if statetements and wrapped my conditionals in the <perch:categories> tag, this now writes out the category title. However my conditionals no longer work, any suggestions?

Updated code:

<perch:categories id="tan-type" label="Tan Type" set="tanning-types" order="4" />
      <span class="small"><perch:category id="catTitle" /></span>

      <perch:if id="tan-type" match="eq" value="light">
        <i class="badge light">&nbsp;</i>
      </perch:if>
      <perch:if id="tan-type" match="eq" value="medium" />
        <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i>
      </perch:if>
      <perch:if id="tan-type" match="eq" value="dark" />
        <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i><i class="badge dark">&nbsp;</i>
      </perch:if>
      <perch:if id="tan-type" match="eq" value="extra-dark" />
        <i class="badge light">&nbsp;</i><i class="badge medium">&nbsp;</i><i class="badge dark">&nbsp;</i><span class="primary-highlight">+</span>
      </perch:if>
    </perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are light, medium, dark etc your categories? If so:

<perch:if id="catSlug" match="eq" value="light">
        <i class="badge light">&nbsp;</i>
</perch:if>

Drew McLellan said:

Are light, medium, dark etc your categories? If so:

<perch:if id="catSlug" match="eq" value="light">
       <i class="badge light">&nbsp;</i>
</perch:if>

Yes they are the categories, thanks for your help.