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.