Forum

Thread tagged as: Question

Categories in page attributes

How should I set a category in page attributes and then use it in a page template?

I've tried this in pages/attributes/default.html:

<perch:categories id="service" label="Which service category does this page relate to?" set="Services" required="true" />
    <perch:category id="catTitle" type="text" />
</perch:categories>
<perch:pages id="header_layout" type="select" label="Choose which header layout to display" options="Header layout 1|global.header_layout1, Header layout 2|global.header_layout2" allowempty="false" required="true" />
...
...  etc for other attributes

Then, in page template, I'm trying to pick up the category/ies in a variable:

$service_category = perch_page_attribute('service', [], true); 
echo $service_category;

This outputs nothing in $service_category.

But, for comparison, if I pull another page attribute into this variable instead in page template:

$service_category = perch_page_attribute('header_layout', [], true);
echo $service_category;

This outputs the value selected for this attribute.

I think the category is being set for the page. So how do I use it in a page template?

Diagnostics

    Perch Runway: 2.8.34, PHP: 5.5.12, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $, with PDO
    Server OS: WINNT, apache2handler
    Installed apps: content (2.8.34), assets (2.8.34), categories (2.8.34), collection_3 (2.8.34)
    App runtimes: <?php $apps_list = array( 'content', 'categories', );
    PERCH_LOGINPATH: /cms
    PERCH_PATH: C:\wamp\www\CLIENT\cms
    PERCH_CORE: C:\wamp\www\CLIENT\cms\core
    PERCH_RESFILEPATH: C:\wamp\www\CLIENT\cms\resources
    Image manipulation: GD
    PHP limits: Max upload 64M, Max POST 50M, Memory: 1024M, Total max file upload: 50M
    F1: 6a33f95eca3667f9e0c39bf5ca2980fe
    Resource folder writeable: Yes
    HTTP_HOST: CLIENT.local
    DOCUMENT_ROOT: C:/wamp/www/CLIENT
    REQUEST_URI: /cms/core/settings/diagnostics/
    SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
Mark Melling

Mark Melling 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll need to output them through a template.

Sorry Drew, but how?

I'm now using a page attributes template service_category.html

<perch:categories id="service" label="Which service does this page relate to?" set="Services">
  <perch:category id="catPath" />
</perch:categories>

And pulling this into my page attributes default.html as follows.

<perch:template path="pages/attributes/service_category.html" />

I can set a category in Page Details for each page.

In my page template:

$service_category = perch_page_attributes(array('template' => 'service_category.html'), true);
$service_category = rtrim($service_category); // trim space or other spurious characters at end of string
$service_category = rtrim($service_category, '/'); // remove forward slash at end of path
echo $service_category; // for testing only; eg services/personal-injury which is the correct format I need for a category path

// display Team relating to a specific category
perch_collection('Team', [
     'template'   => 'team_on_page.html',
     'sort'       => 'team_name',
      'sort-order' => 'ASC',
      // filter by category
      'category' => $service_category
]);

But this returns nothing.

I feel I'm using a massive sledgehammer and failing to crack a nut. Not sure whether all the PHP trimming is required, but it does remove the trailing slash.

echo $service_category; outputs what looks like a valid category path eg services/personal-injury. But when I use $service_category to filter my collection, I get nothing.

And if I filter the collection by typing in a category path instead of using the variable, it works.

What am I doing wrong?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If it works when you hard-code the value but not with the variable, the variable doesn't hold the value you think it does.

Have you turned on debug and checked for the category path you think you're filtering with?

Thanks Drew. There was "\r\n" at the start of the value that was interfering. So I changed a rtrim to a trim and everything appears to be working OK now. My final page template code in case it's of use to anyone else is:

$service_category = perch_page_attributes(array('template' => 'service_category.html'), true); // true returns the templated output
$service_category = trim($service_category); // trim space or other spurious characters from both ends of string
$service_category = rtrim($service_category, '/'); // remove forward slash at end of path
echo $service_category; // for testing only; eg services/personal-injury which is the correct format I need for a category path

// display Team relating to a specific category
    perch_collection('Team', [
        'template'   => 'team_on_page.html',
        'sort'       => 'team_name',
         'sort-order' => 'ASC',
          // filter by category
          'category' => $service_category
     ]);

Quick question. Is this the simplest way to select a category in page attributes and then use it to filter collections in a page template? I'm a bit surprised I have to fiddle around manipulating the PHP variable this way.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that seems very convoluted. I think I would just use skip-template.

Simon Clay

Simon Clay 127 points

Drew I can't see how to use skip-template to return the catPath

$service_category = perch_page_attributes(array(
                'skip-template' => true
            )); // true returns the templated output 

print_r($service_category);

The array returned doesn't contain catPath:

Array
(
    [specialisms] => Array
        (
            [0] => 1
        )

    [description] => Array
        (
            [_flang] => plain
            [raw] => EFM Sport
            [processed] => EFM Sport
        )

    [keywords] => Array
        (
            [_flang] => plain
            [raw] => 
            [processed] => 
        )

    [noindex] => 
    [nofollow] => 
    [nosnippet] => 
    [pageID] => 16
    [pageParentID] => 0
    [pagePath] => /sport
    [pageTitle] => Sport
    [pageNavText] => Sport
    [pageNew] => 0
    [pageOrder] => 7
    [pageDepth] => 1
    [pageSortPath] => /sport
    [pageTreePosition] => 000-009
    [pageSubpageRoles] => 
    [pageSubpagePath] => 
    [pageHidden] => 0
    [pageNavOnly] => 0
    [pageAccessTags] => 
    [pageCreatorID] => 1
    [pageModified] => 2017-04-20 14:39:03
    [pageAttributes] => {"specialisms":["1"],"description":{"_flang":"plain","raw":"EFM Sport","processed":"EFM Sport"},"keywords":{"_flang":"plain","raw":"","processed":""},"noindex":null,"nofollow":null,"nosnippet":null}
    [pageAttributeTemplate] => default.html
    [pageTemplate] => default.php
    [templateID] => 1
    [pageSubpageTemplates] => 
    [pageCollections] => 
    [perch_specialisms] => Array
        (
            [0] => 1
        )

    [perch_description] => Array
        (
            [_flang] => plain
            [raw] => EFM Sport
            [processed] => EFM Sport
        )

    [perch_keywords] => Array
        (
            [_flang] => plain
            [raw] => 
            [processed] => 
        )

    [perch_noindex] => 
    [perch_nofollow] => 
    [perch_nosnippet] => 
)