Forum

Thread tagged as: Question, Problem, Discussion

Can i make an array of perch content checkboxes?

Hi there,

I've have multiple checkboxes that control the output of some text. So associated with a project is a skill set. For instance:

Design, Development.

A user can currently tick to output the text. I wanted to know - can I build an array of these options and then I can use a function to build a comma separated string. Would I simply need to give the inputs a common name and build the array that way?

My code thus far:

<p class="project__meta"><perch:content id="project__meta1" type="checkbox" label="Design" allowempty="true" required="false" suppress="true" value="Design"/>
<perch:if exists="project__meta1">
    Design
</perch:if>
<perch:content id="project__meta2" type="checkbox" label="Developement" allowempty="true" required="false" suppress="true" value="Development" />
<perch:if exists="project__meta2">
    Development
</perch:if>
<perch:content id="project__meta3" type="checkbox" label="Illustration" allowempty="true" required="false" suppress="true" value="Illustration" />
<perch:if exists="project__meta3">
    Illustration
</perch:if>
<perch:content id="project__meta4" type="checkbox" label="Branding" allowempty="true" required="false" suppress="true" value="Branding" />
<perch:if exists="project__meta4">
    Branding
</perch:if>
</p>
Mathew Doidge

Mathew Doidge 2 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What you have there is probably the way to do it, unless you go down the route of using a category set.

Drew McLellan said:

What you have there is probably the way to do it, unless you go down the route of using a category set.

Thanks Drew, The only limitation I've encountered is if I use something like:

<perch:if exists="project__meta3">
    Illustration
    <perch:after>
    &middot;
    </perch:after>
</perch:if>

.. means if I select say the 2nd, 3rd item, I still get the &middot; from the ending 1st item (as the real desire was to separate them by middots). Perhaps I need to take a look at a category set!

Hi Drew,

Thanks for the heads up on using categories. I managed to achieve the desired result by using categories and adjusting my templates.

You're a star!

Simon Clay

Simon Clay 127 points

Hi Mat,

You can do it like this:

<p class="project__meta">
    <perch:content id="project__meta1" type="checkbox" label="Design" value="Design"/>
    <perch:if exists="project__meta1 AND (project__meta2 OR project__meta3 OR project__meta4)">&middot;</perch:if>

    <perch:content id="project__meta2" type="checkbox" label="Developement" value="Development" />
    <perch:if exists="project__meta2 AND (project__meta3 OR project__meta4)">&middot;</perch:if>

    <perch:content id="project__meta3" type="checkbox" label="Illustration" value="Illustration" />
    <perch:if exists="project__meta3 AND project__meta4">&middot;</perch:if>

    <perch:content id="project__meta4" type="checkbox" label="Branding" value="Branding" />
</p>

I have stripped out a few tags that aren't necessary, e.g. perch:if isn't required if you don't use suppress="true", 'required is 'false' by default and 'allowempty' is 'true' by default for checkboxes.

I hope it helps.

Hi Simon,

Nice work. I ended up using categories as my template code then just needed:

<perch:categories id="Skills" label="Skills used on this project" set="project-skill-set">
    <perch:category id="catTitle" /><perch:if exists="perch_item_last"><perch:else /> · </perch:if>
</perch:categories>

Thanks though — I'm sure that'll come in handy sometime :)