Skip to content
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

Open
ghuisman19 opened this issue Nov 1, 2023 · 7 comments
Open

Twig addExtension Uncaught Twig\Error\RuntimeError #203

ghuisman19 opened this issue Nov 1, 2023 · 7 comments

Comments

@ghuisman19
Copy link

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

@kbond
Copy link
Contributor

kbond commented Nov 1, 2023

Hi @ghuisman19, are you using this bundle standalone (not with Symfony)?

@ghuisman19
Copy link
Author

Hi @kbond, yes, i'm using it with the Twig composer package.

@kbond
Copy link
Contributor

kbond commented Nov 1, 2023

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 RuntimeLoaderInterface that can locate the DateTimeFormatter object. Is your app using a PSR-11 container?

Additionally, the DateTimeFormatter will need a translator - do you have that covered?

@ghuisman19
Copy link
Author

We don't use PSR-11. We use a simple autoload function for all composer packages
We just use Twig to render HTML templates.
Like this:

$twig_loader = new \Twig\Loader\FilesystemLoader(self::$twig_path);
self::$twig = new \Twig\Environment($twig_loader);
self::$twig->addExtension(new \Twig\Extra\Intl\IntlExtension());
//self::$twig->addExtension(new \Knp\Bundle\TimeBundle\Twig\Extension\TimeExtension());

// Render Template
self::$twig->createTemplate((string)$this->render)->render($args);

I don't quite understand the question about the translator.

@kbond
Copy link
Contributor

kbond commented Nov 1, 2023

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)

@ghuisman19
Copy link
Author

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.

@kbond
Copy link
Contributor

kbond commented Nov 1, 2023

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');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants