AgentSkillsCN

pw-page-classes

ProcessWire 自定义页面类——通过模板专属逻辑扩展 Page 类。适用于在 ProcessWire 中创建、编辑或处理自定义页面类时使用,或当用户提及页面类、类型化页面,或是模板专属的 PHP 类时使用。

SKILL.md
--- frontmatter
name: pw-page-classes
description: ProcessWire custom page classes — extending the Page class with template-specific logic. Use when creating, editing, or working with custom page classes in ProcessWire, or when the user mentions page classes, typed pages, or template-specific PHP classes.

ProcessWire Custom Page Classes

Custom page classes let you extend ProcessWire's Page with template-specific methods and properties. Every page using a given template becomes an instance of your custom class.

Quick Setup

  1. Enable in /site/config.php:
php
$config->usePageClasses = true;
  1. Create class in /site/classes/ matching the template name:
TemplateClassFile
homeHomePageHomePage.php
blog-postBlogPostPageBlogPostPage.php
basic-pageBasicPagePageBasicPagePage.php

Rule: PascalCase the template name + append Page.

  1. Minimal class:
php
<?php namespace ProcessWire;
class HomePage extends Page {}

Essential Pattern — Custom Properties via get()

php
public function get($key) {
    if($key === 'authorName') return $this->getAuthorName();
    return parent::get($key);
}

This makes $page->authorName work as a virtual property.

API Access Inside Classes

php
// Correct — always use wire()
$this->wire()->pages->find('template=blog-post');
$this->wire('sanitizer')->text($value);

// WRONG — does not work inside page classes
$this->pages;

Property Access Gotcha

Inside the class, $this->property accesses the property directly, bypassing the get() method's lazy loading. For example, $this->template may return null, while $page->template (external) returns a Template object. Use $this->get('property') for consistent behavior.

Reference Files