XF 2.1 override default phrase function

The phrase function isn’t called at run time. It is called at compilation time.

If you look at your compiled templates you will see that templates are compiled for every style and language compilation. So the text for each phrase is essentially hard coded into the compiled template.

What you want to do therefore isn’t possible.
 
@Chris D thanks for your response what i want to do for example if i create a phrase with name "test" and its value is "example" so i want when page will load i want its value should be "1234" i want to do it via addon please let me know how i can do this ?
 
The code is related to template compilation therefore it isn't even extendable by add-ons.

The phrase function is taken care of in src/XF/Template/Compiler/Func/Phrase.php.

This only runs when the template is compiled and you will see that it writes out the content of the phrase directly into the template.
 
As I have just explained, it simply isn’t possible.

What about
PHP:
namespace My\Addon;

class Listener
{
    public static function appSetup(\XF\App $app)
    {
        $app->container()->extend('templateCompiler'] = function(\XF\Template\Compiler $compiler)
        {
            $compiler->setFunction('phrase', '\\My\\Addon\\Template\\Compiler\\Fn\\Phrase');

            return $compiler;
        });
    }
}
?

Though I wouldn't use this in any Add-on outside development systems.
 
I deliberately didn't mention that simply because we explicitly advise against it in the code:
PHP:
// We do not recommend trying to extend this class or the tags/functions it defines. Doing so may interfere
// with the upgrade process. If you must do so, it should not be part of an add-on. It should be done
// unconditionally via config.php. However, do so at your own peril as the worst case would potentially be
// an un-upgradeable installation.
I feel like you already know this as you specifically mention that you wouldn't use it in an add-on ;)

Honestly I'd just avoid extending that in absolutely all contexts.
 
Top Bottom