Collections Display

  • Hi! I have a long list of Collections displaying some basic card-style HTML. I'd like to give toggle controls to my client control over displaying which Collection appear on the page.

    I'm using a "perch_collection" to display the content with a template. But how do I then add a toggle (like a checkbox) to hide or display that Collection? And across all the others without making a ton of regions?

  • drewm

    Approved the thread.
  • Hmm - not sure how I would use that. Here is the situation with an example, perhaps I'm approaching in wrong. I want the the CMS editor to have a choice to select from a bunch of Collections. These Collections are display on the master page, so I can't exactly add a checkbox and some options...can I?

    This example is with some way to add the show/hide class to a div on the master page using region with a bunch of checkboxes.

    1. <div class="hide">
    2. <?php perch_collection('Cards 1', ['template' => 'card.html', ]);?>
    3. </div>
    4. <div class="show">
    5. <?php perch_collection('Cards 2', ['template' => 'card.html', ]);?>
    6. </div>
    7. <div class="show">
    8. <?php perch_collection('Cards 3', ['template' => 'card.html', ]);?>
    9. </div>
  • In your page attribute template, set up an option for each Collection you want to toggle, e.g.

    1. <perch:pages id="cards1" label="Show cards 1" type="checkbox" value="1">

    Then on your page, you can check for it.

    1. <?php
    2. if (perch_page_get_attribute('cards1') == '1') {
    3. perch_collection('Cards 1', ['template' => 'card.html', ]);
    4. }
    5. ?>
  • A while ago I wrote a field type which can be used to select Collection keys. Just published it on GitHub now:


    1. <perch:content id="collections" type="collection" label="Collections">


    1. $collection_keys = perch_content('Collections', true);
    2. $collection_keys = explode(',', $collection_keys);
    3. foreach($collection_keys as $collection_key) {
    4. perch_collection($collection_key, ['template' => 'card.html']);
    5. }
  • @drew - I'm getting this error when implementing your solution:

    1. Fatal error
    2. : Call to undefined function perch_page_get_attribute() in
    3. /home/XXXXXX/public_html/cms/templates/pages/customer-or-partner-portal.php

    And the line it's complaining about:

    1. <?php
    2. if (perch_page_get_attribute('cards1') == '1') {
    3. perch_collection('Resources Cards', ['template' => 'filter-advanced.html', ]);
    4. }
    5. ?>

    And in the Attributes:

    1. <!-- Set Collections -->
    2. <perch:pages id="cards1" label="Show cards 1" type="checkbox" value="1" divider-before="Collections Selector" notes-before="If this page supports Collections, select the ones you want to use here." />

    So what did I miss?

  • That function was added in v3. You'll need to use this instead:

    1. if (perch_page_attribute('cards1', [], true) == '1') {
    2. }

    But you should also update to at least 2.8.34, or your site won't work with newer PHP versions.

  • I have an extension to the question now - can I further filter the displayed Collections using the this method show the cards, but ONLY ones with a particular element from the template?

    So in the Collection is a card with a region called "Tag" - so the content editor adds the words "case" to the tags in the card. Can I then use that specific content from that region to filter the display of the collections?

    (again, in Perch 2.8.29)

    Here's how I am displaying them now without the filter. So my question is for an example on the code with that filter in place.

    1. <?php if (perch_page_attribute('resources-cards-1', [], true) == '1') {perch_collection('Customer Strategy Cards', ['template' => 'filter-grid-cards-low-profile-advanced.html', ]);} ?>
  • Sorry if that's unclear. The idea would be to use a region content - text - to filter the display of a given Collection. I have "cards" in the Collections, which is why I used that term. But it could be anything. Just wondering how to filter Collection displays from within the format I have now.

  • Can you hardcode the filter? If you can and it works as desired, you can make it dynamic:

    1. $filter = perch_page_attribute('resources_filter_field', [], true);
    2. $value = perch_page_attribute('resources_filter_value', [], true);
    3. perch_collection('Examples', [
    4. 'filter' => $field,
    5. 'match' => 'eq',
    6. 'value' => $value,
    7. ]);
  • Thanks Hussein - I'm not sure I have the full snippet correct.

    Where the attribute is "resources-cards-1", the region with the text field is "tags" and the actual filter words I want to use is "case". I added the template for displaying the content. I'm getting an error - what did I miss here? A full tag would be helpful, from open to closing tags, as I can't figure out the piece in between. Thanks for your help!!

  • That was just an example demonstrating that you can use dynamic values for filtering from Page Attributes. You ideally need to check if the page attribute returns a value before you apply the filter.

    Try with hard-coded values. Does it work as you expect?

    1. perch_collection('Examples', [
    2. 'filter' => 'my_field', // field ID in template <perch:content id="my_field">
    3. 'match' => 'eq',
    4. 'value' => 'value I am looking for', // field value
    5. ]);

    I'm getting an error - what did I miss here

    I can't help if I don't know what the error is :S

    the region with the text field is "tags"

    Are you using Regions or Collections?

    p.s. I'm not sure I follow with tags/case. I'm assuming you have a field with the ID tags (and the value you entered for this field is case) :

    1. <perch:content id="tags" ... >

    Anyway, field ID and field value are good descriptive terms. So let's just use these so we're all on the same page.