4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Templating
;
14 use Symfony\Component\Form\AbstractRendererEngine
;
15 use Symfony\Component\Form\FormView
;
16 use Symfony\Component\Templating\EngineInterface
;
19 * @author Bernhard Schussek <bschussek@gmail.com>
21 class TemplatingRendererEngine
extends AbstractRendererEngine
24 * @var EngineInterface
28 public function __construct(EngineInterface
$engine, array $defaultThemes = array())
30 parent
::__construct($defaultThemes);
32 $this->engine
= $engine;
38 public function renderBlock(FormView
$view, $resource, $blockName, array $variables = array())
40 return trim($this->engine
->render($resource, $variables));
44 * Loads the cache with the resource for a given block name.
46 * This implementation tries to load as few blocks as possible, since each block
47 * is represented by a template on the file system.
49 * @see getResourceForBlock()
51 * @param string $cacheKey The cache key of the form view.
52 * @param FormView $view The form view for finding the applying themes.
53 * @param string $blockName The name of the block to load.
55 * @return Boolean True if the resource could be loaded, false otherwise.
57 protected function loadResourceForBlockName($cacheKey, FormView
$view, $blockName)
59 // Recursively try to find the block in the themes assigned to $view,
60 // then of its parent form, then of the parent form of the parent and so on.
61 // When the root form is reached in this recursion, also the default
62 // themes are taken into account.
64 // Check each theme whether it contains the searched block
65 if (isset($this->themes
[$cacheKey])) {
66 for ($i = count($this->themes
[$cacheKey]) - 1; $i >= 0; --$i) {
67 if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->themes
[$cacheKey][$i])) {
73 // Check the default themes once we reach the root form without success
75 for ($i = count($this->defaultThemes
) - 1; $i >= 0; --$i) {
76 if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes
[$i])) {
82 // If we did not find anything in the themes of the current view, proceed
83 // with the themes of the parent view
85 $parentCacheKey = $view->parent
->vars
[self
::CACHE_KEY_VAR
];
87 if (!isset($this->resources
[$parentCacheKey][$blockName])) {
88 $this->loadResourceForBlockName($parentCacheKey, $view->parent
, $blockName);
91 // If a template exists in the parent themes, cache that template
92 // for the current theme as well to speed up further accesses
93 if ($this->resources
[$parentCacheKey][$blockName]) {
94 $this->resources
[$cacheKey][$blockName] = $this->resources
[$parentCacheKey][$blockName];
100 // Cache that we didn't find anything to speed up further accesses
101 $this->resources
[$cacheKey][$blockName] = false;
107 * Tries to load the resource for a block from a theme.
109 * @param string $cacheKey The cache key for storing the resource.
110 * @param string $blockName The name of the block to load a resource for.
111 * @param mixed $theme The theme to load the block from.
113 * @return Boolean True if the resource could be loaded, false otherwise.
115 protected function loadResourceFromTheme($cacheKey, $blockName, $theme)
117 if ($this->engine
->exists($templateName = $theme.':'.$blockName.'.html.php')) {
118 $this->resources
[$cacheKey][$blockName] = $templateName;