|
| 1 | +<!-- |
| 2 | + This source file is part of the open source project |
| 3 | + ExpressionEngine User Guide (https://github.com/ExpressionEngine/ExpressionEngine-User-Guide) |
| 4 | +
|
| 5 | + @link https://expressionengine.com/ |
| 6 | + @copyright Copyright (c) 2003-2023, Packet Tide, LLC (https://packettide.com) |
| 7 | + @license https://expressionengine.com/license Licensed under Apache License, Version 2.0 |
| 8 | +--> |
| 9 | + |
| 10 | +# Developing Variable Modifiers |
| 11 | + |
| 12 | +Add-ons can provide their own [variable modifiers](templates/variable-modifiers.md) for use in templates. |
| 13 | + |
| 14 | +Each variable modifier needs be created as a separate file in the `Modifiers` directory within the add-on's root folder, and registered in `addon.setup.php`. |
| 15 | + |
| 16 | +The file name, which is also the PHP class name, should be the modifier's name with the first letter capitalized. |
| 17 | + |
| 18 | +All modifier files are required to implement `ExpressionEngine\Service\Template\Variables\ModifierInterface`. |
| 19 | + |
| 20 | +Each modifier should have a `namespace` definition, which consists of the add-on's namespace as defined in `addon.setup.php` followed by `\Modifiers`. |
| 21 | + |
| 22 | +Lastly, the modifier's name should be registered in `addon.setup.php`. |
| 23 | + |
| 24 | +TIP: **Tip:** Modifiers provided by add-ons can be called by their name as well as by their name prefixed with add-on's name and underscore. For example, below we can use `{title:hacker}` and `{title:seeo_hacker}` to achieve the same result. |
| 25 | + |
| 26 | +### Example |
| 27 | + |
| 28 | +Let's create the `:hacker` modifier, which would make text look geeky by converting some of the letters to similar looking numbers. This example modifier is part of the "Seeo" add-on. |
| 29 | + |
| 30 | + <?php |
| 31 | + |
| 32 | + /** |
| 33 | + * namespace is required and must be add-on's namespace + 'Modifiers' |
| 34 | + * |
| 35 | + */ |
| 36 | + namespace EEHarbor\Seeo\Modifiers; |
| 37 | + |
| 38 | + use ExpressionEngine\Service\Template\Variables\ModifierInterface; |
| 39 | + |
| 40 | + class Hacker implements ModifierInterface |
| 41 | + { |
| 42 | + public function modify($data, $params = array(), $tagdata = false) |
| 43 | + { |
| 44 | + return str_replace(['e', 'o', 'l'], ['3', '0', '1'], (string) $data); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +Next, we'll add the following to `addon.setup.php` |
| 49 | + |
| 50 | + 'modifiers' => array( |
| 51 | + 'hacker' |
| 52 | + ), |
| 53 | + |
| 54 | +And now, let's call it in a template. |
| 55 | + |
| 56 | + {exp:channel:entries entry_id="1"} |
| 57 | + <div class="title"> |
| 58 | + <span>{title}</span> - Hello |
| 59 | + </div> |
| 60 | + <div class="hacker"> |
| 61 | + <span>{title:hacker}</span> - H3110 |
| 62 | + </div> |
| 63 | + <div class="seeo_hacker"> |
| 64 | + <span>{title:seeo_hacker}</span> - H3110 |
| 65 | + </div> |
| 66 | + {/exp:channel:entries} |
0 commit comments