intro.rst 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 compile your templates to take
  55. advantage of the C extension.
  56. .. tip::
  57. On Windows, you can also simply download and install a `pre-build DLL`_.
  58. Basic API Usage
  59. ---------------
  60. This section gives you a brief introduction to the PHP API for Twig.
  61. The first step to use Twig is to register its autoloader::
  62. require_once '/path/to/lib/Twig/Autoloader.php';
  63. Twig_Autoloader::register();
  64. Replace the ``/path/to/lib/`` path with the path you used for Twig
  65. installation.
  66. .. note::
  67. Twig follows the PEAR convention names for its classes, which means you
  68. can easily integrate Twig classes loading in your own autoloader.
  69. .. code-block:: php
  70. $loader = new Twig_Loader_String();
  71. $twig = new Twig_Environment($loader);
  72. echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));
  73. Twig uses a loader (``Twig_Loader_String``) to locate templates, and an
  74. environment (``Twig_Environment``) to store the configuration.
  75. The ``render()`` method loads the template passed as a first argument and
  76. renders it with the variables passed as a second argument.
  77. As templates are generally stored on the filesystem, Twig also comes with a
  78. filesystem loader::
  79. $loader = new Twig_Loader_Filesystem('/path/to/templates');
  80. $twig = new Twig_Environment($loader, array(
  81. 'cache' => '/path/to/compilation_cache',
  82. ));
  83. echo $twig->render('index.html', array('name' => 'Fabien'));
  84. .. _`download page`: https://github.com/fabpot/Twig/tags
  85. .. _`pre-build DLL`: https://github.com/stealth35/stealth35.github.com/downloads