<commit_before>---
title: Plugin Recipes
page-toc:
  active: true
taxonomy:
    category: docs
---

This page contains an assortment of problems and their respective solutions related to Grav plugins.

## Output some PHP code result in a Twig template

#### Goal:

You want to process some custom PHP code, and make the result available in a page.

#### Solution:

You create a new plugin that creates a Twig extension, and makes some PHP content available in your Twig templates.

Create a new plugin folder in `user/plugins/example`, and add those files:

`user/plugins/example/example.php`
`user/plugins/example/example.yaml`
`user/plugins/example/twig/ExampleTwigExtension.php`

In `twig/ExampleTwigExtension.php` you'll do your custom processing, and return it as a string in `exampleFunction()`.

Then in your Twig template file (or in a page Markdown file if you enabled Twig processing in Pages), render the output using: `{{ example() }}`.

The overview is over, let's see the actual code:

`example.php`:

[prism classes="language-php line-numbers"]
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
class ExamplePlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onTwigExtensions' => ['onTwigExtensions', 0]
        ];
    }
    public function onTwigExtensions()
    {
        require_once(__DIR__ . '/twig/ExampleTwigExtension.php');
        $this->grav['twig']->twig->addExtension(new ExampleTwigExtension());
    }
}
[/prism]

`ExampleTwigExtension.php`:

[prism classes="language-php line-numbers"]
<?php
namespace Grav\Plugin;
class ExampleTwigExtension extends \Twig_Extension
{
    public function getName()
    {
        return 'ExampleTwigExtension';
    }
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('example', [$this, 'exampleFunction'])
        ];
    }
    public function exampleFunction()
    {
        return 'something';
    }
}
[/prism]

`example.yaml`:
<commit_msg>TwigExtension is deprecated in 1.7 (#949)<commit_after>---
title: Plugin Recipes
page-toc:
  active: true
taxonomy:
    category: docs
---

This page contains an assortment of problems and their respective solutions related to Grav plugins.

## Output some PHP code result in a Twig template

#### Goal:

You want to process some custom PHP code, and make the result available in a page.

#### Solution:

You create a new plugin that creates a Twig extension, and makes some PHP content available in your Twig templates.

Create a new plugin folder in `user/plugins/example`, and add those files:

`user/plugins/example/example.php`
`user/plugins/example/example.yaml`
`user/plugins/example/twig/ExampleTwigExtension.php`

In `twig/ExampleTwigExtension.php` you'll do your custom processing, and return it as a string in `exampleFunction()`.

Then in your Twig template file (or in a page Markdown file if you enabled Twig processing in Pages), render the output using: `{{ example() }}`.

The overview is over, let's see the actual code:

`example.php`:

[prism classes="language-php line-numbers"]
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
class ExamplePlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onTwigExtensions' => ['onTwigExtensions', 0]
        ];
    }
    public function onTwigExtensions()
    {
        require_once(__DIR__ . '/twig/ExampleTwigExtension.php');
        $this->grav['twig']->twig->addExtension(new ExampleTwigExtension());
    }
}
[/prism]

`ExampleTwigExtension.php`:

[prism classes="language-php line-numbers"]
<?php
namespace Grav\Plugin;
use Grav\Common\Twig\Extension\GravExtension;

class ExampleTwigExtension extends GravExtension
{
    public function getName()
    {
        return 'ExampleTwigExtension';
    }
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('example', [$this, 'exampleFunction'])
        ];
    }
    public function exampleFunction()
    {
        return 'something';
    }
}
[/prism]

`example.yaml`:

{'fraction_non_alphanumeric': 0.10510877536054754, 'fraction_numerical': 0.0017110730872647274, 'mean_word_length': 3.5618729096989967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4253416', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1153, 'n_words': 395}