text.rst 1.6 KB

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