PHPackages                             bechwebbkonsult/wordpress-template-models - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. bechwebbkonsult/wordpress-template-models

ActiveLibrary[Templating &amp; Views](/categories/templating)

bechwebbkonsult/wordpress-template-models
=========================================

Template models in Wordpress

0.0.1(7y ago)024MITPHPPHP ^7.0

Since Sep 24Pushed 4mo agoCompare

[ Source](https://github.com/bechwebbkonsult/wordpress-template-models)[ Packagist](https://packagist.org/packages/bechwebbkonsult/wordpress-template-models)[ RSS](/packages/bechwebbkonsult-wordpress-template-models/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Template models in Wordpress
============================

[](#template-models-in-wordpress)

Template models are class methods that is executed immediately before WordPress includes the predetermined template file. If you have data that you want to be bound to a template each time that template is rendered, a template model can help you organize that logic into a single location.

Usage
-----

[](#usage)

A template model is a class where you can put some complex logic for your templates. You can create a template model by extending the provided `Bechwebb\TemplateModels\TemplateModel`.

```
class HomeTemplateModel extends TemplateModel
{
    public $terms = [];

    public function __construct()
    {
        foreach (get_terms(['taxonomy' => 'bf_calendar', 'hide_empty' => false]) as $term) {
            $term->url = get_term_link($term->term_id);
            $this->terms[] = $term;
        }
    }
}
```

All template models need to be registered to the desired wordpres template.

```
use Bechwebb\TemplateModels\TemplateModelProvider;

$templateModelProvider = new TemplateModelProvider;
$templateModelProvider->register('/home.php', \App\TemplateModels\HomeTemplateModel::class);
```

In the template all public properties from the template model is now available.

```
