bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Register token filter don't works

gmzu opened this issue · comments

commented

I create a new class, as explained in API reference

 class Helper extends \Prefab {
	function badwords($val) {
		$bad_words = array("badword","jerk","damn");
		$replacement_words = array("@#$@#", "j&*%", "da*@"); 
		return str_ireplace($bad_words, $replacement_words, $val);
	}
}

I later registered new filter in a controller class -> beforeRoute function with

\Preview::instance()->filter('badwords','\Helper::instance()->badwords');

In a template file I use {{@text | badwords}} but in the line in which I use the new filter I get an Invalid method NULL
As if the registration of the filter had not happened
No errors if I don't use the filter in the template
Tested on php 7.3.9 and 7.4.2

I guess it's just the call here... try
\Preview::instance()->filter('badwords','Helper->badwords');

commented

it doesn't work

commented

After much testing I managed to get it to work with

\Template::instance()->filter('badwords','Helper->badwords')

commented

After other tests, I verified that it really only works like that (I was misled by the cache)

\Template::instance()->filter('badwords','Helper::badwords')

obviously you have to declare the functions as static.
In the other case it always gives an invalid '->' object operator error in template temp file

commented

makes sense as you do not want to create new instances all the time :)