intro.rst 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.*"
  49. }
  50. }
  51. 3. Install via composer
  52. .. code-block:: bash
  53. php composer.phar install
  54. .. note::
  55. If you want to learn more about Composer, the ``composer.json`` file syntax
  56. and its usage, you can read the `online documentation`_.
  57. Installing the C extension
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. .. versionadded:: 1.4
  60. The C extension was added in Twig 1.4.
  61. Twig comes with a C extension that enhances the performance of the Twig
  62. runtime engine. You can install it like any other PHP extension:
  63. .. code-block:: bash
  64. $ cd ext/twig
  65. $ phpize
  66. $ ./configure
  67. $ make
  68. $ make install
  69. Finally, enable the extension in your ``php.ini`` configuration file:
  70. .. code-block:: ini
  71. extension=twig.so
  72. And from now on, Twig will automatically compile your templates to take
  73. advantage of the C extension. Note that this extension does not replace the
  74. PHP code but only provides an optimized version of the
  75. ``Twig_Template::getAttribute()`` method.
  76. .. tip::
  77. On Windows, you can also simply download and install a `pre-build DLL`_.
  78. Basic API Usage
  79. ---------------
  80. This section gives you a brief introduction to the PHP API for Twig.
  81. The first step to use Twig is to register its autoloader::
  82. require_once '/path/to/lib/Twig/Autoloader.php';
  83. Twig_Autoloader::register();
  84. Replace the ``/path/to/lib/`` path with the path you used for Twig
  85. installation.
  86. If you have installed Twig via Composer you can take advantage of Composer's
  87. autoload mechanism by replacing the previous snippet for::
  88. require_once '/path/to/vendor/autoload.php'
  89. .. note::
  90. Twig follows the PEAR convention names for its classes, which means you
  91. can easily integrate Twig classes loading in your own autoloader.
  92. .. code-block:: php
  93. $loader = new Twig_Loader_String();
  94. $twig = new Twig_Environment($loader);
  95. echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));
  96. Twig uses a loader (``Twig_Loader_String``) to locate templates, and an
  97. environment (``Twig_Environment``) to store the configuration.
  98. The ``render()`` method loads the template passed as a first argument and
  99. renders it with the variables passed as a second argument.
  100. As templates are generally stored on the filesystem, Twig also comes with a
  101. filesystem loader::
  102. $loader = new Twig_Loader_Filesystem('/path/to/templates');
  103. $twig = new Twig_Environment($loader, array(
  104. 'cache' => '/path/to/compilation_cache',
  105. ));
  106. echo $twig->render('index.html', array('name' => 'Fabien'));
  107. .. _`download page`: https://github.com/fabpot/Twig/tags
  108. .. _`online documentation`: http://getcomposer.org/doc
  109. .. _`pre-build DLL`: https://github.com/stealth35/stealth35.github.com/downloads