2

I had own Template class which did something like this:

    public function render(array $context)
    {
        // App & Twig
        $app = App::getInstance();
        $twig = $app->twig;

        // Figure out which textdomain should be used
        $prevTextDomain = textdomain(null);
        textdomain($twig->getRenderTextDomain());

        // Added helper variables, like isAdmin, isApi, isFront, am(assetmanager).
        $context = array_merge($context, $twig->getRenderAttributes());

        // Render
        $data = parent::render($context);

        // Set back to previous text domain
        if (isset($prevTextDomain))
        {
            textdomain($prevTextDomain);
        }

        return $data;
    }

But now this is deprecated, but I've not seen any new way of doing this? There must be, as why would they just deprecate stuff without giving new way of achieve the same? So how one should do this without base_template_class?

I've seen some talks that doEnterNode/doLeaveNode hooks is new recommended way. But what I understand that, I could probably implement that textdomain logic there, but how to get custom variables to context?

Also doEnterNode/doLeaveNode sounds adding more overhead than just overwriting render.

So the question is, how to achieve this without base_template_class?


UPDATE 7.1.2020

No solution still, but maybe I should comment what workaround I did.

It's not the perfect solution, but in my case at least now it seemed to be enough.

As I don't think I'm calling template's render in cases that matters, so changed all my template->render calls to be myTwig->render calls that does the textdomain things and calls twig->render.

But if there is cases where template would call another template's render, and it would need to use different textdomain then I think this doesn't work and to that I don't have any solution to give. So leaving this still open.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
jmto
  • 291
  • 3
  • 7
  • With `myTwig` u mean u've created a custom class that extends from `TwigEnvironment` right? – DarkBee Jan 08 '20 at 13:01
  • That's probably also option, but can't remember is it. Twig has sometimes had some "final" here and there to make you not to do that. Quick look to Twig's source looks like it's also an option. But I just did a class, that has __construct that puts the Environment to $this->_twig and then calls that if needed. It also has __call that calls methods from there if they exists if we haven't overwritten the Twig one. – jmto Jan 09 '20 at 20:02
  • Went through the `doEnterNode` and `doLeaveNode` hooks but can't wrap my head around it at this point. I'll give it another look on these node visitors actually work – DarkBee Jan 10 '20 at 05:43
  • What I looked those, their names sounds already wrong. I don't want to do something for every node, I want to do something for every template. But ok, if they are only way doing what I want, I think I could live with the overhead of calling it every node. But quick look and they seem to get node and env, is there possible to get the template? Would need testing, but it doesn't seem that they work, or it at least isn't obvious or properly documented. Some kind of TemplateVisitor with `doEnterTemplate` and `doLeaveTemplate` would sound more like what I was doing. – jmto Jan 11 '20 at 19:47
  • Don't know if you have ever solved this? [Here](https://stackoverflow.com/questions/69881387/adding-debug-traces-to-symfony-3-4-twig-templates-during-after-render?rq=1) is another topic about this – DarkBee Jun 08 '22 at 05:46

0 Answers0