Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

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

Solved by Karmalakas View solution

Started by Jose 1 year ago · 5 replies · 115 views
1 year ago

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:
    https://twig.symfony.com/doc/3.x/filters/format_currency.html

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
    JS
    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.
https://twig.symfony.com/doc/1.x/

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

1 year ago

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 🤷‍♂️

1 year ago

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.

1 year ago

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.

1 year ago

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 🙏, it worked fine 😊

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 😊
Regards

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 197 2 months ago
Themes & Styling · by Ian, 2 months ago
3 92 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 127 3 months ago