Jump to content

Observations on pageClass


Recommended Posts

This is not really a question or a tutorial, but merely an observation in case it helps anyone else.

I have a site which I built before the custom page classes became available in the core, using a similar approach to this: https://processwire.com/docs/tutorials/using-custom-page-types-in-processwire/

To achieve this, I set the pageClass property of the relevant templates to be the same as the template name.

I have now refactored the site to use the core custom page class functionality. I didn't change the pageClass property. Until then, I hadn't realised that $template->pageClass and $template->getPageClass() would yield different results, but they do.

For instance, if the template name is 'Collection' and the pageClass property is set on the system tab to be 'Collection':

  • $template->pageClass is 'Collection'
  • $template->getPageClass() is:
    • 'Collection' if that class exists (this was the case in my initial implementation - hence why I didn't realise the difference between pageClass and getPageClass() )
    • 'Page' if the Collection class does not exist and $config->usePageClasses is not set
    • 'CollectionPage' if that class exists, the Collection class does not exist and $config->usePageClasses = true
    • 'Page' if the Collection and CollectionPage classes do not exist and $config->usePageClasses = true

In the last two cases, getPageClass() will output a warning "Template '$template' page class '$pageClass' is not available"

Removing the pageClass property from the template results in:

  • $template->pageClass is ''
  • $template->getPageClass() is:
    • 'Page' if $config->usePageClasses is not set
    • 'CollectionPage' if that class exists and $config->usePageClasses = true
    • 'Page' if the CollectionPage class does not exist and $config->usePageClasses = true

In this case, no warnings are output.

Bottom line: if you are migrating from the 'old' approach to the use of core custom page classes, beware of any instances of $template->pageClass as you may need to change them to $template->getPageClass().

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...