|
| 1 | +.. index:: |
| 2 | + single: Templating |
| 3 | + |
| 4 | +The Templating Component |
| 5 | +===================== |
| 6 | + |
| 7 | + The Templating Component is a PHP template engine providing template |
| 8 | + inheritence and object-oriented helper functions. |
| 9 | + |
| 10 | +Installation |
| 11 | +------------ |
| 12 | + |
| 13 | +You can install the component in many different ways: |
| 14 | + |
| 15 | +* Use the official Git repository (https://github.com/symfony/Templating); |
| 16 | +* Install it via PEAR ( `pear.symfony.com/Templating`); |
| 17 | +* Install it via Composer (`symfony/templating` on Packagist). |
| 18 | + |
| 19 | +Usage |
| 20 | +----- |
| 21 | + |
| 22 | +The :class:`Symfony\\Component\\Templating\\PhpEngine` class is the entry point |
| 23 | +of the component. It needs a template name parser |
| 24 | +(:class:`Symfony\\Component\\templating\\TemplateNameParserInterface`) to |
| 25 | +convert a template name to a template reference and template loader |
| 26 | +(:class:`Symfony\\Component\\templating\\Loader\\LoaderInterface`) to find the |
| 27 | +template associated to a reference. |
| 28 | + |
| 29 | + use Symfony\Component\Templating\PhpEngine; |
| 30 | + use Symfony\Component\Templating\TemplateNameParser; |
| 31 | + use Symfony\Component\Templating\Loader\FilesystemLoader; |
| 32 | + |
| 33 | + $loader = new FilesystemLoader(__DIR__ . '/views/%name%'); |
| 34 | + |
| 35 | + $view = new PhpEngine(new TemplateNameParser(), $loader); |
| 36 | + |
| 37 | + echo $view->render('hello.php', array('firstname' => 'Fabien')); |
| 38 | + |
| 39 | +The :method:`Symfony\\Component\\Templating\\PhpEngine::render` method executes |
| 40 | +the file `views/hello.php` and returns the output text. |
| 41 | + |
| 42 | +.. code-block::php |
| 43 | +
|
| 44 | + <!-- views/hello.php --> |
| 45 | + Hello, <?php echo $firstname ?>! |
| 46 | +
|
| 47 | +
|
| 48 | +Template inheritence with slots |
| 49 | +------------------------------- |
| 50 | + |
| 51 | +The template inheritence is designed to share layouts with many templates. |
| 52 | + |
| 53 | +.. code-block::php |
| 54 | +
|
| 55 | + <!-- views/layout.php --> |
| 56 | + <html> |
| 57 | + <head> |
| 58 | + <title><?php $view['slots']->output('title', 'Default title') ?></title> |
| 59 | + </head> |
| 60 | + <body> |
| 61 | + <?php $view['slots']->output('_content') ?> |
| 62 | + </body> |
| 63 | + </html> |
| 64 | +
|
| 65 | +The :method:`Symfony\\Templating\\PhpEngine::extend` method is called in the |
| 66 | +sub-template to set its parent template. |
| 67 | + |
| 68 | +.. code-block::php |
| 69 | +
|
| 70 | + <!-- views/page.php --> |
| 71 | + <?php $view->extend('layout.php') ?> |
| 72 | +
|
| 73 | + <?php $view['slots']->set('title', $page->title) ?> |
| 74 | +
|
| 75 | + <h1> |
| 76 | + <?php echo $page->title ?> |
| 77 | + </h1> |
| 78 | + <p> |
| 79 | + <?php echo $page->body ?> |
| 80 | + </p> |
| 81 | +
|
| 82 | +To use template inheritence, the :class:`Symfony\\Templating\\Helper\\SlotsHelper` |
| 83 | +helper must be registered. |
| 84 | + |
| 85 | + use Symfony\Templating\Helper\SlotsHelper; |
| 86 | + |
| 87 | + $view->set(new SlotsHelper()); |
| 88 | + |
| 89 | + // Retrieve $page object |
| 90 | + |
| 91 | + echo $view->render('page.php', array('page' => $page)); |
| 92 | + |
| 93 | +.. note:: |
| 94 | + |
| 95 | + Multiple levels of inheritence are possible: a layout can extend an other |
| 96 | + layout. |
| 97 | + |
0 commit comments