---
title: 'Twig filters example'
url: 'https://getgrav.org/forum/archive/twig-filters-example-t1489'
markdown: 'https://getgrav.org/forum/archive/twig-filters-example-t1489.md'
date: '2026-06-21'
description: 'Would be much appreciated an example of a plugin that add custom twig filters. Thanks'
---

[Archive](https://getgrav.org/forum/archive)

#  Twig filters example 

  Started by Muut Archive 12 years ago · 4 replies · 635 views    

 [12 years ago](https://getgrav.org/forum/archive/twig-filters-example-t1489#post-291 "Permalink") 

Would be much appreciated an example of a plugin that add custom twig filters.
Thanks

 [12 years ago](https://getgrav.org/forum/archive/twig-filters-example-t1489#post-292 "Permalink") 

There are two main parts to this question:

1. First you need to create a Twig extension file that provides the filters. An example of this can be found in [TwigExtension.php](https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/TwigExtension.php) file that we provide in the Grav core. You can use this as an example, as it has quite a few handy filters in it. You can just create your own and provide it with the plugin.
2. The next part is the plugin itself, it really only needs to provide a method for the `onTwigExtensions` event, and that is where you would add the extension with something like:

```php
require_once(__DIR__ . '/CustomTwigFilters.php';
$this->grav['twig']->twig->addExtension(new CustomTwigFilters());
```

Obviously this assumes you have a created a Twig extension called CustomTwigFilters and saved it in a file called CustomTwigFilters.php.

Something like that should work fine.

 [12 years ago](https://getgrav.org/forum/archive/twig-filters-example-t1489#post-293 "Permalink") 

Thanks, I tried this, but I got this error :

Call to a member function addExtension() on a non-object

on the line : $this->grav\['twig'\]->twig->addExtension(new CustomTwigFilters());

the class CustomTwigFilters() is being required and seems to be fine.

 [12 years ago](https://getgrav.org/forum/archive/twig-filters-example-t1489#post-294 "Permalink") 

I'll need to see all your code for the plugin to know what's going on.

 [12 years ago](https://getgrav.org/forum/archive/twig-filters-example-t1489#post-295 "Permalink") 

you need to change CustomTwigFilters() with \\Grav\\Common\\CustomTwigFilters() because you are in this namespace :)

[Log in](https://getgrav.org/login?return=%2Fforum%2Farchive%2Ftwig-filters-example-t1489) to reply.

###  Suggested topics

 | Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| [About the Archive category](https://getgrav.org/forum/archive/about-the-archive-category-t6942)    [Archive](https://getgrav.org/forum/archive) · by Deleted User, 9 years ago |  | 0 | 2853 | [9 years ago](https://getgrav.org/forum/archive/about-the-archive-category-t6942 "by Deleted User") |
| [Hey buddies!](https://getgrav.org/forum/archive/hey-buddies-t643)    [Archive](https://getgrav.org/forum/archive) · by Muut Archive, 9 years ago |  | 2 | 1870 | [9 years ago](https://getgrav.org/forum/archive/hey-buddies-t643 "by Muut Archive") |
| [There is a way to do a "if condition" in MD page?](https://getgrav.org/forum/archive/there-is-a-way-to-do-a-if-condition-in-md-page-t3377)    [Archive](https://getgrav.org/forum/archive) · by Muut Archive, 9 years ago |  | 2 | 5193 | [9 years ago](https://getgrav.org/forum/archive/there-is-a-way-to-do-a-if-condition-in-md-page-t3377 "by Muut Archive") |
| [Official docker image of grav (+admin +gitsync)](https://getgrav.org/forum/archive/official-docker-image-of-grav-admin-gitsync-t8519)    [Archive](https://getgrav.org/forum/archive) · by Muut Archive, 9 years ago |  | 1 | 4010 | [9 years ago](https://getgrav.org/forum/archive/official-docker-image-of-grav-admin-gitsync-t8519 "by Muut Archive") |
| [Weird Grav + caddy + iOS + https problem](https://getgrav.org/forum/archive/weird-grav-caddy-ios-https-problem-t7837)    [Archive](https://getgrav.org/forum/archive) · by Muut Archive, 9 years ago |  | 3 | 1958 | [9 years ago](https://getgrav.org/forum/archive/weird-grav-caddy-ios-https-problem-t7837 "by Muut Archive") |
