Nette 2.2: registering filters

5 years ago

robix
Member | 4
+
0
-

Hi,
I have been trying for several hours to understand the new way Latte is integrated in Nette 2.2 but I got completely lost in the docs, APIs and sources. Many topics seem to be quite related to my problem but I haven't found an answer.

I would like to apply my own filter on the template before Latte is applied. In Nette 2.1, I used to redefine the templatePrepareFilters() in my BasePresenter like this:

public function templatePrepareFilters($template)
{
    $template->registerFilter(new \MyExt\MyCustomFilter());
    $latte = new Nette\Latte\Engine;
    $template->registerFilter($latte);
}

Now in 2.2, the registerFilter is deprecated. What's the recommended replacement?
Thanks for any hint!

5 years ago

Milo
Nette Core | 1128
+
+2
-

MartyIX: New Latte filter is the synonym for the old Latte helper. So helpers = filters now.

robix: Old template filters are not supported/implemented in the alone Latte. You can check the current deprecated behaviour and copy it by your own. Maybe someone else give you a better hint :(

5 years ago

robix
Member | 4
+
0
-

Thanks for reply. I have been looking into the code and it seems that in 2.2.1, Latte is directly bound to the template used in the presenter. I.e. for using something different, I have to create another Template class and change the rendering. However, it seems that Latte reads the template source by itself now so I can see no space for integrating some pre-processing filter :-(

5 years ago

David Grudl
Nette Core | 6829
+
0
-

You can use for loading StringLoader, see https://github.com/…oEscape.phpt#L14

5 years ago

robix
Member | 4
+
0
-

That was it! Now it's solved. I created my own Loader that implements the pre-filtering and I register it in startup() of my BasePresenter:

public function startup()
{
    parent::startup();
    $this->template->getLatte()->setLoader(new \My\OwnLoader($this));
}

Or is there any more suitable place to set the loader?
Many thanks!

5 years ago

David Grudl
Nette Core | 6829
+
0
-

You can do it in startup(), little bit better is to use createTemplate():

protected function createTemplate()
{
    $template = parent::createTemplate();
    $template->getLatte()->setLoader(new My\OwnLoader($this));
    return $template;
}

Because this is very new part of Nette and default Loader is under development, inherit your loader from the original one (for forward compatibility):

class OwnLoader extends Nette\Bridges\ApplicationLatte\Loader
{

    public function getContent($file)
    {
        $content = parent::getContent($file);
        // modify $content
        return $content;
    }

}

5 years ago

robix
Member | 4
+
0
-

Yes, I did it exactly like that. Thanks for help!