Trying to format currency using Twig, I found: "Warning Twig version 1.x is no longer maintained."

Hello everyone, I wish you all a successful and happy 2025.

  1. I was following this twig documentation to format a numeric frontmatter field as currency with locale es:
    format_currency - Filters - Documentation - Twig PHP

But the number field disappeared.

  1. Since I saw the note on versions, I looked for the twig version that Grav uses and I found it in: /public_html/vendor/twig/twig/src/Environment.php
class Environment
{
    public const VERSION = '1.44.8';
    public const VERSION_ID = 14408;
    public const MAJOR_VERSION = 1;
    public const MINOR_VERSION = 44;
    public const RELEASE_VERSION = 8;
    public const EXTRA_VERSION = '';

2.2 I was trying to find the way to format it as currency in Twig 1.x but on the documentation page I got a discouraging warning:

Warning Twig version 1.x is no longer maintained.
Documentation - Twig - The flexible, fast, and secure PHP template engine

3.1 Why Grav uses such an outdated version of Twig? is it safe and reliable?

3.2 How can I easily format the display of a frontmatter numeric field as currency? I looked into Grav documentation, but I was not able to find any “currency format”

3.3 I began to understand the power of using Twig, but how can I upgrade Twig to version 3 without breaking something in Grav?

Thank you and regards
joejac

Reading the docs I understand, that 2.x would work fine, just might produce some deprecation errors. To upgrade to 3.x would require getting rid of all these errors. 1.x is probably there since the beginning of Grav :man_shrugging:

Thank you for the explanation @Karmalakas but since I am not a programmer I do not want to mess with the core installation of Grav, so is there a workaround using Twig 1.44.8 to format numbers in this way?:

If in the frontmatter page.header.product.price contains 1000000 it should show 1.000.000 with the separator dots in the webpage, the decimal figures are not needed. I want to avoid using javascript. I prefer using Twig because I guess I can better handle locales and currencies also using the languages.yaml files

Thanks.

This maybe?

1 Like

v3.x might not come any time soon apparently:

Also, we can move from Twig 1 to Twig 2, but unfortunately not to Twig 3. The problem with our Twig requirement is that plugins and themes rely on the ability to use the Twig Defer extension to add CSS/JS assets to themes, but this extension is not compatible (and may never be) with Twig 3. Upgrading to Twig 3 would effectively break every Grav site, so until we have a suitable solution, we cannot entertain a move to Twig 3.

Thank you very much @Karmalakas and I apologize, I did not look, I thought Twig 1.x did not have that function, very friendly on your part for your kindness in sending me the link :pray:, it worked fine :blush:

Also thanks for the reference to 1.8 Beta of Grav, I only saw in the documentation up to 1.7. Now I understand the problem to upgrade. For me is enough this 1.x function:

{{ (-9800.333)|number_format(2, '.', ',') }} {# outputs : -9,800.33 #}

hopefully, 1.x does not have any security issues.

You saved my day @Karmalakas :blush:
Regards