smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.23k stars 703 forks source link

Re-add support for implode, substr and json_encode #939

Closed wisskid closed 5 months ago

wisskid commented 5 months ago

v4 deprecates and v5 drops support for many native php functions. implode, substr and json_encode are commonly used and it might be useful to support them.

wisskid commented 5 months ago

There is a problem with implode. Using implode (or join) as a modifier in templates (prior to Smarty v5) (on PHP8), has to be done as follows: <td class="{' '|implode:$classes|escape}">, since passing the separator after the array is no longer supported in PHP.

As a modifier, this seems silly. It would be more intuitive to flip the arguments, in the way Twig does it: {{ [1, 2, 3]|join('|') }}. But we cannot just flip the arguments. We could detect argument types and throw a deprecation warning.

wisskid commented 5 months ago

It seems Smarty v5 also supports strlen, time, count, is_array and in_array as functions. (They are implemented as FunctionHandlers.) This is undocumented.

wisskid commented 5 months ago

Smarty also supports explode. It's undocumented, but it has been in the unit tests. However, the parameter order is weird and we cannot auto-fix it, since both parameters are strings. I propose to deprecate explode and promote split with the parameters reversed. For consistency, this would also mean we should implement join in favor of implode.