This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

rajakhoury
rajakhoury

How do you register twig extensions in a plugin. I want to add this package to my app : https://github.com/nochso/html-compress-twig

The package has filters, functions and tags.

I have been trying for hours without success. Here's an attempt

use nochso\HtmlCompressTwig\Extension;

public function registerMarkupTags()
 {
       return [
        'functions' => [
            'htmlcompress' =>  [(new Extension()), 'getFunctions'],
        ]
    ];
 }

Error : The "HtmlCompressTwig" extension is not enabled.

Or

     return [
        'functions' => [
            (new Extension())->getFunctions()
        ]
    ];

Error : An exception has been thrown during the compilation of a template ("The markup function for 0 is not callable.").

The getFunctions() in Extensions.php is

 public function getFunctions()
   {
    return array(
        new \Twig_SimpleFunction('htmlcompress', $this->callable, $this->options),
     );
 }

Anyone ?

KurtJensen
KurtJensen

In your Plugin.php file do like:

public function registerMarkupTags()
{
    return [
        'functions' => [
            'can' => function ($lock) {return $this->can($lock);},
            'inGroup' => function ($group) {return $this->inGroup($group);},
        ],
    ];
}

private function inGroup($group)
{
    if (!$user = Auth::getUser()) {
        return false;
    }

    return ($user->groups->where('name', $group)->first()) ? true : false;
}

private function can($lock)
{
    return in_array($lock, self::passage_keys());
}
Cryden
Cryden
use nochso\HtmlCompressTwig\Extension;
use Event;

public function boot()
    {
        Event::listen('cms.page.beforeRenderPage', function($controller, $page) {
            $twig = $controller->getTwig();
            $twig->addExtension(new Extension(true));
        });
    }

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.