2

After switching to PHP 8.2 and Smarty 4.3, I get a lot of deprecation warnings like

PHP Deprecated: Using php-function "substr" as a modifier is deprecated and will be removed in a future release. Use Smarty::registerPlugin to explicitly register a custom modifier

This message comes for substr, rand, mt_rand, json_encode and others.

Why where the PHP Functions removed, and is there an easy way to get the function back from the Smarty Repository or does every user of Smarty has to write these functions on his own?

They where really useful ...

3
  • 2
    The functions haven't been removed, "support for using PHP functions as modifiers" has been removed in Smarty. You probably need to create/use Plugins to continue using your functionality Commented Sep 25, 2023 at 8:07
  • @brombeer Of course they did not remove the PHP Functions but the support. Thanks for the link, the decision seemed to be quite controversial. Commented Sep 25, 2023 at 8:33
  • To understand why the support has been removed, look at github.com/smarty-php/smarty/discussions/967 Some workarounds can also be found Commented Apr 4, 2024 at 9:23

1 Answer 1

6

It can be solved in a very simple way, you just need to do this:

$smarty->registerPlugin("modifier", "substr", "substr");
$smarty->registerPlugin("modifier", "rand", "rand");
$smarty->registerPlugin("modifier", "json_encode", "json_encode");

And the same for every modifier you need. The other option is to replace the modifier with the use of the function, so this:

{$var|substr:0:10}

To this:

{substr($var,0,10)}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.