intro.rst 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 via Composer
  40. ~~~~~~~~~~~~~~~~~~~~~~~
  41. 1. Install composer in your project:
  42. .. code-block:: bash
  43. curl -s http://getcomposer.org/installer | php``
  44. 2. Create a ``composer.json`` file in your project root:
  45. .. code-block:: javascript
  46. {
  47. "require": {
  48. "twig/twig": "1.6.0"
  49. }
  50. }
  51. 3. Install via composer
  52. .. code-block:: bash
  53. php composer.phar install
  54. Installing the C extension
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. .. versionadded:: 1.4
  57. The C extension was added in Twig 1.4.
  58. Twig comes with a C extension that enhances the performance of the Twig
  59. runtime engine. You can install it like any other PHP extension:
  60. .. code-block:: bash
  61. $ cd ext/twig
  62. $ phpize
  63. $ ./configure
  64. $ make
  65. $ make install
  66. Finally, enable the extension in your ``php.ini`` configuration file:
  67. .. code-block:: ini
  68. extension=twig.so
  69. And from now on, Twig will automatically compile your templates to take
  70. advantage of the C extension.
  71. .. tip::
  72. On Windows, you can also simply download and install a `pre-build DLL`_.
  73. Basic API Usage
  74. ---------------
  75. This section gives you a brief introduction to the PHP API for Twig.
  76. The first step to use Twig is to register its autoloader::
  77. require_once '/path/to/lib/Twig/Autoloader.php';
  78. Twig_Autoloader::register();
  79. Replace the ``/path/to/lib/`` path with the path you used for Twig
  80. installation.
  81. .. note::
  82. Twig follows the PEAR convention names for its classes, which means you
  83. can easily integrate Twig classes loading in your own autoloader.
  84. .. code-block:: php
  85. $loader = new Twig_Loader_String();
  86. $twig = new Twig_Environment($loader);
  87. echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));
  88. Twig uses a loader (``Twig_Loader_String``) to locate templates, and an
  89. environment (``Twig_Environment``) to store the configuration.
  90. The ``render()`` method loads the template passed as a first argument and
  91. renders it with the variables passed as a second argument.
  92. As templates are generally stored on the filesystem, Twig also comes with a
  93. filesystem loader::
  94. $loader = new Twig_Loader_Filesystem('/path/to/templates');
  95. $twig = new Twig_Environment($loader, array(
  96. 'cache' => '/path/to/compilation_cache',
  97. ));
  98. echo $twig->render('index.html', array('name' => 'Fabien'));
  99. .. _`download page`: https://github.com/fabpot/Twig/tags
  100. .. _`pre-build DLL`: https://github.com/stealth35/stealth35.github.com/downloads