1The Text Extension 2================== 3 4The Text extension provides the following filters: 5 6* ``truncate`` 7* ``wordwrap`` 8 9Installation 10------------ 11 12First, :ref:`install the Extensions library<extensions-install>`. Next, add 13the extension to Twig:: 14 15 $twig->addExtension(new Twig_Extensions_Extension_Text()); 16 17Wrapping Words 18-------------- 19 20Use the ``wordwrap`` filter to split your text in lines with equal length. 21 22.. code-block:: jinja 23 24 {{ "Lorem ipsum dolor sit amet, consectetur adipiscing"|wordwrap(10) }} 25 26This example would print:: 27 28 Lorem ipsu 29 m dolor si 30 t amet, co 31 nsectetur 32 adipiscing 33 34The default separator is "\\n", but you can easily change that by providing one: 35 36.. code-block:: jinja 37 38 {{ "Lorem ipsum dolor sit amet, consectetur adipiscing"|wordwrap(10, "zz\n") }} 39 40This would result in:: 41 42 Lorem ipsuzz 43 m dolor sizz 44 t amet, cozz 45 nsectetur zz 46 adipiscing 47 48Truncating Text 49--------------- 50 51Use the ``truncate`` filter to cut off a string after limit is reached 52 53.. code-block:: jinja 54 55 {{ "Hello World!"|truncate(5) }} 56 57The example would output ``Hello...``, as ``...`` is the default separator. 58 59You can also tell truncate to preserve whole words by setting the second 60parameter to ``true``. If the last Word is on the the separator, truncate 61will print out the whole Word. 62 63.. code-block:: jinja 64 65 {{ "Hello World!"|truncate(7, true) }} 66 67Here ``Hello World!`` would be printed. 68 69If you want to change the separator, just set the third parameter to 70your desired separator. 71 72.. code-block:: jinja 73 74 {{ "Hello World!"|truncate(7, false, "??") }} 75 76This example would print ``Hello W??``. 77