-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Twig addExtension Uncaught Twig\Error\RuntimeError #203
Comments
Hi @ghuisman19, are you using this bundle standalone (not with Symfony)? |
Hi @kbond, yes, i'm using it with the Twig composer package. |
Ah ok, understood. In version 2 of this package, we switched to using a twig runtime for performance reasons. So when using standalone, in addition to the extension, you also need to register a Additionally, the |
We don't use PSR-11. We use a simple autoload function for all composer packages $twig_loader = new \Twig\Loader\FilesystemLoader(self::$twig_path); // Render Template I don't quite understand the question about the translator. |
Ok, here's what will work for you (but you still need a translator - see comment): self::$twig->addExtension(new \Knp\Bundle\TimeBundle\Twig\Extension\TimeExtension());
self::$twig->addRuntimeLoader(new Twig\RuntimeLoader\FactoryRuntimeLoader([
Knp\Bundle\TimeBundle\DateTimeFormatter::class => function() {
return new DateTimeFormatter(???); // you need to inject an instance of \Symfony\Contracts\Translation\TranslatorInterface
}
])); See https://github.com/symfony/translation/tree/6.3#translation-component for creating one but you'd need to load the translation key/values provided by this bundle (or just create your own) |
Thanks for the explanation. I have no experience at all with sympony translations. So I can't get that working with the translations in KNBlabs package. In Twig i have this string diff.ago.hour. But with the addRuntimeLoader Twig can at least do the rendering, the Uncaught Twig\Error\RuntimeError is solved. |
If you're only dealing with English, there aren't many translations you'd need. You could use the example to fill them in by looking at this file: $translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
'diff.ago.hour' => '1 hour ago|%count% hours ago',
// ... all "source"/"targets" from this the translation resource file
], 'en'); |
Hi,
Maybe I'm doing something wrong, but I can't tell from the description. I add the time extension to Twig, but then I get an Uncaught Twig\Error\RuntimeError error.
$twig->addExtension(new \Knp\Bundle\TimeBundle\Twig\Extension\TimeExtension());
Uncaught Twig\Error\RuntimeError: Unable to load the "Knp\Bundle\TimeBundle\DateTimeFormatter" runtime in "_string_template
The text was updated successfully, but these errors were encountered: