Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarty::setExtensions() killing registered filters #1006

Open
SlowFox71 opened this issue Apr 30, 2024 · 6 comments
Open

Smarty::setExtensions() killing registered filters #1006

SlowFox71 opened this issue Apr 30, 2024 · 6 comments

Comments

@SlowFox71
Copy link

Instructions to add output filters are:

$smarty->registerFilter("output", "protect_email");

Instructions to add custom extensions (overriding the defaults) are:

$smarty->setExtensions([
    new Smarty\Extension\CoreExtension(),
    new MyCustomExtension(),
    new Smarty\Extension\DefaultExtension(),
]);

Unfortunately the latter kills $smarty->BCPluginsAdapter thus disabling any filters. I solved the problem by using:

$extensions = $smarty->getExtensions();
array_splice($extensions, 1, 0, [new MyCustomExtension]);
$this->setExtensions($extensions);

I don't like the style, but at least it is working. But something must be done here, either in the API, or at least the documentation should be updated.

@wisskid
Copy link
Contributor

wisskid commented Apr 30, 2024

Yes, if you also want the BCPluginsAdapter, you could also do this:

$smarty->setExtensions([
    new Smarty\Extension\CoreExtension(),
    new MyCustomExtension(),
    new Smarty\Extension\DefaultExtension(),
   new Smarty\Extension\BCPluginsAdapter($smarty),
]);

I didn't document that because I thought people who write custom Extensions and insert them before the default ones probably don't need the BCPluginsAdapter anymore.

@SlowFox71
Copy link
Author

Ok.

But am I really supposed to create (yet another) extension, just to register a filter? This seems kind of overkill to me.

@wisskid
Copy link
Contributor

wisskid commented Apr 30, 2024

No, you don't need to. But if you are creating a custom extension anyway, why not put everything you need in there?

@SlowFox71
Copy link
Author

The custom extension is created and included in my framework.

One single call in the application using that framework needs a special output filter, and it took me quite a while to figure out why it is ignored.

What else besides the (deprecated?) registerFilter() could be used for the filter?

@wisskid
Copy link
Contributor

wisskid commented Apr 30, 2024

Add

	public function getOutputFilters(): array {
		return [ ];
	}

to your MyCustomExtension and return an object that implements \Smarty\Filter\FilterInterface.

For example:

class MyReverseFilter implements \Smarty\Filter\FilterInterface {

	public function filter($code, \Smarty\Template $template) {
		return strrev($code);
	}

}

Btw: you are right about the documentation. It needs more love.

@SlowFox71
Copy link
Author

From a logical point of view MyCustomExtension is the wrong place. Perhaps I'll create a second, controller-specific extension, I'll have to think about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants