Mar 16

october cms extend custom twig markup

OctoberCMS has a rich feature set and it's really easy to extend It. But sometimes some features are not directly extendable because it is not possible to satisfy 100% of need.

So to overcome that need we need to find some smart workarounds :)

For Ex: OctoberCMS allow use to add custom twig functions and twig filter for markup. but unfortunately, it is not allowing us to add twig test.

But can easily extend this feature and add our own twig test so we can use them in conditions.

for this, you need to listen for event where we can extend twig environment and add our custom test.

In your plugin.php file you can add below code to extend OctoberCMS `twig`
// other imports
use TwigExtensionAbstractExtension as TwigExtension;

// our extension class 
// you can declare here inside plugin.php file or in your `pluginclasses` file
// for simplicity we have declared it here

class MyTwigExtension extends TwigExtension {
    public function getTests() {
        return [
            new TwigTwigTest('numeric', function ($value) {
                return is_numeric($value);

class Plugin extends PluginBase {
    public function boot() {
            function ($controller, $url, $page) {
                $controller->getTwig()->addExtension(new MyTwigExtension);
    // other code

Once you add extension code you can start using isNumeric in the page markup area

Within page you can use it as below
{% if 12 is numeric %}
    yes numeric.
{% else %}
    not a numeric.
{% endif %}

please share your thoughts in the comments, and stay tuned for new OctoberCMS blogs.

october cms extend custom twig markup