intro.rst 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Introduction
  2. ============
  3. This is the documentation for Twig, the flexible, fast, and secure template
  4. engine for PHP.
  5. If you have any exposure to other text-based template languages, such as
  6. Smarty, Django, or Jinja, you should feel right at home with Twig. It's both
  7. designer and developer friendly by sticking to PHP's principles and adding
  8. functionality useful for templating environments.
  9. The key-features are...
  10. * *Fast*: Twig compiles templates down to plain optimized PHP code. The
  11. overhead compared to regular PHP code was reduced to the very minimum.
  12. * *Secure*: Twig has a sandbox mode to evaluate untrusted template code. This
  13. allows Twig to be used as a template language for applications where users
  14. may modify the template design.
  15. * *Flexible*: Twig is powered by a flexible lexer and parser. This allows the
  16. developer to define its own custom tags and filters, and create its own DSL.
  17. Prerequisites
  18. -------------
  19. Twig needs at least **PHP 5.2.4** to run.
  20. Installation
  21. ------------
  22. You have multiple ways to install Twig. If you are unsure what to do, go with
  23. the tarball.
  24. Installing from the tarball release
  25. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. 1. Download the most recent tarball from the `download page`_
  27. 2. Unpack the tarball
  28. 3. Move the files somewhere in your project
  29. Installing the development version
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. 1. Install Subversion or Git
  32. 2. For Git: ``git clone git://github.com/fabpot/Twig.git``
  33. 3. For Subversion: ``svn co http://svn.twig-project.org/trunk/ twig``
  34. Installing the PEAR package
  35. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. 1. Install PEAR
  37. 2. ``pear channel-discover pear.twig-project.org``
  38. 3. ``pear install twig/Twig`` (or ``pear install twig/Twig-beta``)
  39. Installing the C extension
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. .. versionadded:: 1.4
  42. The C extension was added in Twig 1.4.
  43. Twig comes with a C extension that enhances the performance of the Twig
  44. runtime engine. You can install it like any other PHP extension:
  45. .. code-block:: bash
  46. $ cd ext/twig
  47. $ phpize
  48. $ ./configure
  49. $ make
  50. $ make install
  51. Finally, enable the extension in your ``php.ini`` configuration file:
  52. .. code-block:: ini
  53. extension=twig.so
  54. And from now on, Twig will automatically compiles your templates to take
  55. advantage of the C extension.
  56. Basic API Usage
  57. ---------------
  58. This section gives you a brief introduction to the PHP API for Twig.
  59. The first step to use Twig is to register its autoloader::
  60. require_once '/path/to/lib/Twig/Autoloader.php';
  61. Twig_Autoloader::register();
  62. Replace the ``/path/to/lib/`` path with the path you used for Twig
  63. installation.
  64. .. note::
  65. Twig follows the PEAR convention names for its classes, which means you
  66. can easily integrate Twig classes loading in your own autoloader.
  67. .. code-block:: php
  68. $loader = new Twig_Loader_String();
  69. $twig = new Twig_Environment($loader);
  70. echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));
  71. Twig uses a loader (``Twig_Loader_String``) to locate templates, and an
  72. environment (``Twig_Environment``) to store the configuration.
  73. The ``render()`` method loads the template passed as a first argument and
  74. renders it with the variables passed as a second argument.
  75. As templates are generally stored on the filesystem, Twig also comes with a
  76. filesystem loader::
  77. $loader = new Twig_Loader_Filesystem('/path/to/templates');
  78. $twig = new Twig_Environment($loader, array(
  79. 'cache' => '/path/to/compilation_cache',
  80. ));
  81. echo $twig->render('index.html', array('name' => 'Fabien'));
  82. .. _`download page`: https://github.com/fabpot/Twig/tags