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

Function declared, but NOT #959

Closed KarelWintersky closed 4 months ago

KarelWintersky commented 4 months ago

PHP 7.4, Smarty 4.5.1

$this->smarty->registerPlugin('function', 'getenv', 'getenv');

then

var_dump($smarty->registered_plugins);

returns:

array(2) {
  ["function"]=>
  array(1) {
    ["getenv"]=>
    array(3) {
      [0]=>
      string(6) "getenv"
      [1]=>
      bool(true)
      [2]=>
      array(0) {
      }
    }
  }
}

but:

Using unregistered function "getenv" in a template is deprecated and will be removed in a future release. Use Smarty::registerPlugin to explicitly register a custom modifier.

KarelWintersky commented 4 months ago

Used somewhere in templates like:

{if getenv('ENV_STATE') == 'dev'}
 ... 
{else}
...
{/if}
wisskid commented 4 months ago

The error message could have been clearer: you nees to register it as a modifier, not as a function.

KarelWintersky commented 4 months ago

It works. Thank you. Is it declared anywhere in documentation?

wisskid commented 4 months ago

https://smarty-php.github.io/smarty/stable/api/extending/modifiers/

KarelWintersky commented 4 months ago

But it's is not a like modifier...

{if getenv('ENV_STATE') == 'dev'}
 ... 
{else}
...
{/if}
AlexPashley commented 4 months ago

I think modifiers are classed as both {$var|get_env} and {if get_env()}{/if}

if it was {getEnv} it would be a block not a modifier https://smarty-php.github.io/smarty/stable/api/extending/block-tags/

Gemorroj commented 4 months ago

@KarelWintersky it's variant of modifier. see https://smarty-php.github.io/smarty/stable/designers/language-modifiers/#using-modifiers-in-expressions but I agree, it's not obvious and at first glance it looks like a mistake.