PHPackages                             hoku/twig-wordpress - 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. hoku/twig-wordpress

Abandoned → [timber/timber](/?search=timber%2Ftimber)ArchivedLibrary[Templating &amp; Views](/categories/templating)

hoku/twig-wordpress
===================

The Twig implementation for WordPress themes

v0.3.0(8y ago)12952BSD-3-ClausePHPPHP &gt;=5.2.4

Since Nov 22Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jonasblomdin/Twig-Wordpress)[ Packagist](https://packagist.org/packages/hoku/twig-wordpress)[ RSS](/packages/hoku-twig-wordpress/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Twig-Wordpress
==============

[](#twig-wordpress)

[![Maintenance](https://camo.githubusercontent.com/8118bd09ba8fa31a7323ca2a4e996d63b1c7f35d5f7f2a8447b0372edfac18e1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61696e7461696e65642533462d6e6f2d7265642e737667)](https://bitbucket.org/lbesson/ansi-colors)[![No Maintenance Intended](https://camo.githubusercontent.com/d904056147052e22d8e1c7f46bb50293ed2aeb4c43ead9a2d0cf7a48b46d0562/687474703a2f2f756e6d61696e7461696e65642e746563682f62616467652e737667)](http://unmaintained.tech/)

This is no longer supported, please consider using [Timber](https://github.com/timber/timber) instead.
An implementation which aims to bring the [Twig Template Engine](http://twig.sensiolabs.org) to [Wordpress](http://wordpress.org) with flexibility and as little hassle as possible.

Install
-------

[](#install)

The recommended way to install Twig-Wordpress is via Composer.
Install composer in your project:

```
curl -s http://getcomposer.org/installer | php
```

Create a composer.json file in your project root:

```
{
  "require": {
    "hoku/twig-wordpress": "dev-master"
  }
}
```

Install via composer.

```
php composer.phar install
```

To get started, we create the folder structure that's' necessary inside your theme for Twig-Wordpress to work.
You can change this to whatever you like.

```
your-theme
  twig
    templates
      index.html.twig
      ..

```

Define your constants and then require the autoloader for composer.
To match the folder structure above, put this inside your *functions.php*.

```
define('TWP___TWIG_ROOT', get_template_directory().'/twig/');
define('TWP___TEMPLATE_PATH', TWP___TWIG_ROOT.'templates/'); // This could be omitted because it's the default
require_once '/path/to/vendor/autoload.php';
```

Templates
---------

[](#templates)

The [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy) used is the same as for Wordpress. The file extension is twig though, instead of php.
The bootstrap loads the correct template based on it's hierarchy and if the template file exists. So make sure your template exists in your [TWP\_\_\_TEMPLATE\_PATH](#twp___template_path).

##### Using The Loop

[](#using-the-loop)

[The Loop](http://codex.wordpress.org/The_Loop) is very central for Wordpress. As a result, I've tried to make an implementation for use in Twig.
The *loop* property is an instance of the *TWP\_Loop* class, which is an iterator with Wordpress flavor.
It's *current* method will return the current global *$post*, after [the\_post](http://codex.wordpress.org/Function_Reference/the_post) has been called.

```
{% for post in loop %}
  {{ post.post_title }}
{% else %}
  Nothing to read.
{% endfor %}
```

You can also use [the\_post](http://codex.wordpress.org/Plugin_API/Action_Reference/the_post) action to alter the post specific properties.

```
function my_post($post)
{
  $post->content = get_the_content();
}
add_action('the_post', 'my_post');
```

With that in place, you can now use the altered post properties in Twig.

```
{% for post in loop %}
  {{ post.post_title }}

    {{ post.content|raw }}

{% else %}
  Nothing to read.
{% endfor %}
```

##### Custom templates

[](#custom-templates)

Every post, regardless of type, can also use [Custom Templates](http://codex.wordpress.org/Page_Templates). They are configured similarly to Wordpress custom templates in that they have a particular style of Twig comment at the top of them.
You can specify both name and which post types your custom template should be available for. The *Template Name* is required and the *Post Type* is optional. If no *Post Type* is provided, the template will be available for all post types.

```
{#
Template Name: My Custom Template
Post Type: page, post
#}
```

Constants
---------

[](#constants)

##### TWP\_\_\_ADMIN

[](#twp___admin)

Twig-Wordpress admin flag. Use it to activate, when [TWP\_\_\_CACHE\_PATH](#twp___cache_path) also has been set, an admin menu item which can be used to clear the Twig cache. Defaults to true.

##### TWP\_\_\_CACHE\_PATH

[](#twp___cache_path)

Twig cache path. A writeable folder for your Twig template cache.

##### TWP\_\_\_CUSTOM\_TEMPLATE

[](#twp___custom_template)

Twig-Wordpress custom templates meta name. Defaults to "\_wp\_twig\_template".

##### TWP\_\_\_CUSTOM\_TEMPLATE\_TYPES

[](#twp___custom_template_types)

Twig-Wordpress custom templates types. Use it to override which post types that should have the custom template options or use false to disable custom templates. The value must be either false or a serialized array with the post types as values. Defaults to a serialized array containing the values page and post.

##### TWP\_\_\_DEBUG

[](#twp___debug)

Twig debug flag. Defaults to *WP\_DEBUG* constant value. Note that enabling [TWP\_\_\_DEBUG](#twp___debug) disables the Twig template cache, even though [TWP\_\_\_CACHE\_PATH](#twp___cache_path) is set.

##### TWP\_\_\_DOMAIN

[](#twp___domain)

Twig-Wordpress domain used for i18n. Defaults to "default".

##### TWP\_\_\_TWIG\_ROOT

[](#twp___twig_root)

Twig root path. Defaults to a folder named "twig", including a trailing slash, within the Twig-Wordpress directory. When requesting your Twig templates, they should be relative to this folder.

##### TWP\_\_\_TEMPLATE\_PATH

[](#twp___template_path)

Twig-Wordpress template path. Defaulta to a folder named "templates", including a trailing slash, within [TWP\_\_\_TWIG\_ROOT](#twp___twig_root). By default, Twig will look in this folder for every template during bootstrap.

Actions
-------

[](#actions)

##### TWP\_\_init

[](#twp__init)

This action is triggered just after the *TWP\_Environment* is instantiated. It provides the environment instance and params as it's parameters.
This example adds the [Debug Extension](http://twig.sensiolabs.org/doc/extensions/debug.html) to Twig.

```
function my_init($twig, $params)
{
  $twig->addExtension(new Twig_Extension_Debug());
}
add_action('TWP__init', 'my_init', 1, 2);
```

This example adds an additional param to Twig, which can be used in your templates.

```
function my_init($twig, $params)
{
  $params['home'] = get_bloginfo('url');
}
add_action('TWP__init', 'my_init', 1, 2);
```

##### TWP\_\_environemnt

[](#twp__environemnt)

This action is triggered in the constructor of *TWP\_Environment*. It provides the environment instance as it's only parameter.
This example adds the [query\_posts](http://codex.wordpress.org/Function_Reference/query_posts) function to Twig.

```
function my_environment($environment)
{
  $environment->addFunction('query_posts', new Twig_Function_Function('query_posts'));
}
add_action('TWP__environemnt', 'my_environment');
```

Filters
-------

[](#filters)

##### TWP\_\_options

[](#twp__options)

This filter runs just before the *TWP\_Environment* is instantiated. It provides the environment options as it's only parameter.
Remember that this filter is executed after the [TWP\_\_\_DEBUG](#twp___debug) and [TWP\_\_\_CACHE\_PATH](#twp___cache_path) constants has been assigned to the options, so changes here will override them.
This example disables auto-escaping for Twig.

```
function my_options($options)
{
  $options['autoescape'] = false;
  return $options;
}
add_filter('TWP__options', 'my_options');
```

##### TWP\_\_templates\_list

[](#twp__templates_list)

This filter runs when the custom Twig templates has been fetched. This example adds a custom template for pages, which doesn't have a Twig comment at the top.

```
function my_templates_list($templates, $post_type)
{
  if ($post_type == 'page') {
    $templates['my-custom-page-template-without-comment.html.twig'] = __('My Custom Page Template Without Comment');
  }
  return $templates;
}
add_filter('TWP__templates_list', 'my_templates_list', 1, 2);
```

##### TWP\_\_template

[](#twp__template)

This filter runs when the Twig template has been found.
It provides the template path, index and a boolean representation for if the template is loaded by index.php or not.
This example loads 'happy-new-year.html.twig' as the template on new years eve.

```
function my_template($name, $index, $root)
{
  if (date('m/d', time()) == '12/31') {
    return 'happy-new-year.html.twig';
  }
  return $name;
}
add_filter('TWP__template', 'my_template', 1, 3);
```

##### TWP\_\_template\_(type)

[](#twp__template_type)

The template filters runs when the default Twig templates are declared.
This example changes the 404 template name to *not-found.html.twig*.

```
function my_404_template($filename)
{
  return 'not-found.html.twig';
}
add_filter('TWP__template_404', 'my_404_template');
```

The following template filters are available.

- TWP\_\_template\_404
- TWP\_\_template\_search
- TWP\_\_template\_taxonomy
- TWP\_\_template\_front-page
- TWP\_\_template\_home
- TWP\_\_template\_attachment
- TWP\_\_template\_single
- TWP\_\_template\_page
- TWP\_\_template\_category
- TWP\_\_template\_tag
- TWP\_\_template\_author
- TWP\_\_template\_date
- TWP\_\_template\_archive
- TWP\_\_template\_comments-popup
- TWP\_\_template\_paged
- TWP\_\_template\_index

##### TWP\_\_template\_(type)-override

[](#twp__template_type-override)

The override template filter runs when the Twig templates, wihich override the defaults, are declared.
Note that each overridable template filter need to include "%s", which is a placeholder for the object property.
This example changes the taxonomy override template, which now can be found in the folder "override".

```
function my_taxonomy_template_override($filename)
{
  return 'override/'.$filename;
}
add_filter('TWP__template_taxonomy-override', 'my_taxonomy_template_override');
```

The following overridable template filters are available.

- TWP\_\_template\_taxonomy-override
- TWP\_\_template\_single-override
- TWP\_\_template\_page-override
- TWP\_\_template\_category-override
- TWP\_\_template\_tag\_override
- TWP\_\_template\_author-override
- TWP\_\_template\_archive-override

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~464 days

Total

4

Last Release

3211d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/80740?v=4)[Jonas Blomdin](/maintainers/jonasblomdin)[@jonasblomdin](https://github.com/jonasblomdin)

---

Top Contributors

[![jonasblomdin](https://avatars.githubusercontent.com/u/80740?v=4)](https://github.com/jonasblomdin "jonasblomdin (41 commits)")

---

Tags

deprecatedwordpresstwigtemplating

### Embed Badge

![Health badge](/badges/hoku-twig-wordpress/health.svg)

```
[![Health](https://phpackages.com/badges/hoku-twig-wordpress/health.svg)](https://phpackages.com/packages/hoku-twig-wordpress)
```

###  Alternatives

[timber/timber

Create WordPress themes with beautiful OOP code and the Twig Template Engine

5.7k3.7M128](/packages/timber-timber)[craftcms/cms

Craft CMS

3.6k3.6M3.0k](/packages/craftcms-cms)[symfony/ux-twig-component

Twig components for Symfony

22018.6M330](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1647.0M122](/packages/symfony-ux-live-component)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

16126.1k1](/packages/symfony-ux-toolkit)[mati365/ckeditor5-symfony

CKEditor 5 integration for Symfony

261.9k](/packages/mati365-ckeditor5-symfony)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
