Jump to content

Twig Extensions


justb3a
 Share

Recommended Posts

ProcessWire Twig Extensions

Allows customizing twig, e.g. add extensions.

Dependencies:

Instructions:

After installation go to module settings and enable/disable the required extensions/functions.

Includes:

Twig Extensions Repository

Usage:

Adds the following extensions/functions:

  • Debug // only if debug mode is turned on

    {# dump - dumps information about a template variable #}
    {{ dump('Hello World!') }}
    string(12) "Hello World!"
  • Intl

    {# localizeddate - format dates into a localized string representating the date #}
    {{ "now"|date_modify("-2 day")|localizeddate('medium', 'none', 'en') }}
    Mar 8, 2017
    
    {# localizednumber - format numbers into a localized string representating the number #}
    {{ '50.5555'|localizednumber('decimal', 'default', 'en') }}
    50.556
    
    {# localizedcurrency - format a currency value into a localized string #}
    {{ '50.5555'|localizedcurrency('EUR', 'en') }}
    €50.56
  • Text

    {# truncate - cut off a string after limit is reached #}
    {{ 'Hello World!'|truncate(5) }}
    Hello...
    
    {# wordwrap - split your text in lines with equal length #}
    {{ 'Hello World!'|wordwrap(4) }}
    Hell
    o Wo
    rld!
  • Array

    {# shuffle - randomize an array #}
    {{ [ 'one', 'two', 'three', 'four' ]|shuffle }}
    [ 'two', 'three', 'one', 'four' ]
  • Date

    {# time_diff - difference between two dates #}
    {% set start = "now"|date_modify("-2 day") %}
    {% set end = "now"|date_modify("+2 day") %}
    {{ start|time_diff }}
    2 days ago
    
    {{ start|time_diff(end) }}
    4 days ago
  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...

Hi @justb3a,

I just noticed when I upgraded to the latest version of TemplateEngineTwig that I'm now getting an error message.  I think the TwigExtensions vendor library might possibly need to be upgraded in this module as well.

On https://github.com/twigphp/Twig-extensions it say's it is up to 1.5.1.  This module includes 1.3.  TemplateEngineTwig upgraded Twig from 1.24.2 to 1.35

I just manually tried to "composer update" in the TwigExtensions module directory, but now I get a different error:

error.thumb.jpg.ebd8de266fbe8676e2ffed8af9bb8bb1.jpg

Do you have any ideas as to why I'm getting these errors?

Link to comment
Share on other sites

  • 4 weeks later...

This is not working for me! I'm using the following:

  • Template Engine Factory 1.1.3
  • Template Engine Twig 1.1.0

I get this error:

User Error Exception: An exception has been thrown during the compilation of a template ("Object of class Twig_Source could not be converted to string") in {}. (in C:\MAMP\htdocs\twig\site\assets\cache\FileCompiler\site\modules\TemplateEngineTwig\TemplateEngineTwig.module line 94)

Since I'm new to PW, I'm afraid I can't be much help!

Link to comment
Share on other sites

@V7dev - Twig Extensions module seems to work fine with Template Engine Factory 1.1.3 and Template Engine Twig 1.0.4

Hopefully @justb3a can take a look some time soon to see why it's not working with Template Engine Twig 1.1.0?

I'm still trying to figure it out, but I'm not making much progress.  :(

Link to comment
Share on other sites

@gmclelland - That's odd, I just installed a fresh copy of TE Factory and TE Twig with the latest Twig Extensions and I get an internal server error; I disable the Twig Extensions, the site works fine - not sure how I could have messed up such a simple procedure, but I wouldn't put it past me! :)

Link to comment
Share on other sites

@justb3a I was able to fix this module to work with the latest version of TemplateEngineTwig by modifying TwigExtensions/composer.json to:

{
    "require": {
        "twig/extensions": "~1.5",
        "twig/twig": "~1.35"
    }
}

and then run composer update.

Twig also gets installed when composer install is ran because it is a dependency of Twig Extensions Library.  So anytime the TemplateEngineTwig module updates it's version of Twig, this module will also need it's version to match.

Here's the pull request. https://github.com/justb3a/processwire-twigextensions/pull/11

I think we also should remove the debug extension.  That's included in the TemplateEngineTwig module now.  See https://github.com/wanze/TemplateEngineTwig/commit/776d3d8b3cf2fd04ea584d92fdc33e214468996b#diff-174a5e0bac30f021aa66bb8899550f87R55

I'll try to create a pull request for this fix as well.

Hope that helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...