Skip to content

πŸͺͺ Models

Overview

A value built from a post's or term's own data, such as a person's full name or the outputs related to them, belongs on a model: a class extending Timber\Post or Timber\Term. Controllers then hand those models to the view unchanged, and the template reads the value as a property.

Models are your crew manifest β€” each one knows its own details, so nobody else has to work them out.

Computed Properties with #[Property]

Mark a method with #[Property] and Twig can read it as a property:

src/Models/Person.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace MyTheme\Models;

use PressGang\Attributes\Memoize;
use PressGang\Attributes\Property;
use PressGang\Quartermaster\Quartermaster;
use Timber\Post;

class Person extends Post {

    #[Property]
    protected function get_full_name(): string {
        return trim( $this->meta( 'first_name' ) . ' ' . $this->meta( 'last_name' ) );
    }

    #[Property]
    #[Memoize]
    protected function get_related_outputs(): array {
        return Quartermaster::posts( 'output' )
            ->whereMetaLikeAny( 'people_involved', [ $this->ID ] )
            ->status( 'publish' )
            ->toArray();
    }
}

views/single-person.twig

<h1>{{ post.full_name|e }}</h1>

{% for output in post.related_outputs %}
    <a href="{{ output.link|e('url') }}">{{ output.title|e }}</a>
{% endfor %}
  • The property name is the method name without get_. Pass a name, as in #[Property( 'name' )], to use a different one.
  • No trait or base class is needed. PressGang answers Timber's own meta lookup, so {{ post.full_name }}, $post->full_name and $post->meta( 'full_name' ) all reach the method.
  • Only marked methods are exposed. A get_… helper without the attribute stays private to the class.
  • Methods must be public or protected and take no required parameters; anything else throws a LogicException on first read.
  • A method may wrap the stored field it is named after. Inside get_first_name(), $this->meta( 'first_name' ) returns the stored value, not the method's own result.
  • A null result stays null. It never falls back to a stored meta value of the same name.
  • Overrides keep the attribute. A child model that overrides a marked method does not need to repeat it.

Computing once with #[Memoize]

A template often reads the same property more than once, for example in an {% if %} and then a {% for %}. Add #[Memoize] to compute it once per model instance. Use it for methods that run a query; a method that only joins a few strings does not need it. A direct $this->get_related_outputs() call always runs the method.

Map the model

Tell Timber which class to build in config/timber-class-map.php:

config/timber-class-map.php

return [
    'post' => [
        'post'   => \PressGang\Post::class,
        'person' => \MyTheme\Models\Person::class,
    ],
];

This file replaces the parent's map, so re-declare 'post' => \PressGang\Post::class alongside your own entries.

The HandlesDynamicGetters Trait

HandlesDynamicGetters is the convention-based alternative to #[Property]. A model that uses the trait exposes every get_{field}() method as a property, with nothing to mark:

use PressGang\Traits\HandlesDynamicGetters;

class Person extends \Timber\Post {

    use HandlesDynamicGetters;

    protected function get_full_name(): string { /* ... */ }
}

It remains fully supported, and #[Memoize] works with it too. Prefer #[Property] for new models, because exposure is explicit; keep the trait where a model already uses it, or where you want the differences below.

#[Property] HandlesDynamicGetters
Which methods are exposed Only those marked Every get_{field}() method
Reading in Twig Same Same
$post->field in PHP when the result is empty false, as for any Timber meta read The method's own value
$post->field in PHP, read twice Stored on the object after the first read Runs the method each time, unless #[Memoize]
Later timber/{type}/meta filters Run on the result Do not run
Static typing with the PHPStan extension Typed Typed

Use one form per model.

Note

#[Property] is the model counterpart to #[Context] on controllers: in both, the method owns the fetching and the attribute declares that it is template API.