Skip to content

Commit

Permalink
[Form][FrameworkBundle] PHP theming
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 22, 2011
1 parent e717c99 commit f39ce67
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 31 deletions.
@@ -0,0 +1,9 @@
<tr>
<td>
<?php echo $view['form']->label($form, isset($label) ? $label : null) ?>
</td>
<td>
<?php echo $view['form']->errors($form) ?>
<?php echo $view['form']->widget($form) ?>
</td>
</tr>
@@ -0,0 +1,7 @@
<?php if (0 < count($errors)) : ?>
<tr>
<td colspan="2">
<?php echo $view['form']->renderBlock('field_errors'); ?>
</td>
</tr>
<?php endif; ?>
@@ -0,0 +1,5 @@
<table <?php echo $view['form']->renderBlock('container_attributes') ?>>
<?php echo $view['form']->renderBlock('field_rows') ?>
<?php echo $view['form']->rest($form) ?>
</table>

@@ -0,0 +1,6 @@
<tr style="display: none">
<td colspan="2">
<?php echo $view['form']->widget($form) ?>
</td>
</tr>

92 changes: 64 additions & 28 deletions src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php
Expand Up @@ -25,19 +25,31 @@
*/
class FormHelper extends Helper
{
static protected $cache = array();

protected $engine;

protected $varStack;

protected $context;

public function __construct(EngineInterface $engine)
protected $resources;

protected $themes;

/**
* Constructor;
*
* @param EngineInterface $engine The templating engine
* @param array $resources An array of theme name
*/
public function __construct(EngineInterface $engine, array $resources = array())
{
$this->engine = $engine;
$this->varStack = array();
$this->context = array();
$this->themes = new \SplObjectStorage();

$this->resources = 0 == count($resources) ? array('FrameworkBundle:Form') : $resources;

}

public function isChoiceGroup($label)
Expand All @@ -50,6 +62,19 @@ public function isChoiceSelected(FormView $view, $choice)
return FormUtil::isChoiceSelected($choice, $view->get('value'));
}

/**
* Sets a theme for a given view.
*
* The theme format is "<Bundle>:<Controller>".
*
* @param FormView $view A FormView instance
* @param string|array $resources A theme or an array of theme
*/
public function setTheme(FormView $view, $themes)
{
$this->themes[$view] = (array) $themes;
}

/**
* Renders the HTML enctype in the form tag, if necessary.
*
Expand Down Expand Up @@ -178,28 +203,31 @@ protected function renderSection(FormView $view, $section, array $variables = ar
if (isset($this->varStack[$rendering])) {
$typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
$types = $this->varStack[$rendering]['types'];
$this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
$variables = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
} else {
$types = $view->get('types');
$types[] = $custom;
$typeIndex = count($types) - 1;
$this->varStack[$rendering] = array (
'variables' => array_replace_recursive($view->all(), $variables),
'types' => $types,
);
$variables = array_replace_recursive($view->all(), $variables);
$this->varStack[$rendering]['types'] = $types;
}

$this->varStack[$rendering]['variables'] = $variables;

do {
$types[$typeIndex] .= '_'.$section;
$template = $this->lookupTemplate($types[$typeIndex]);
$template = $this->lookupTemplate($view, $types[$typeIndex]);

if ($template) {

$this->varStack[$rendering]['typeIndex'] = $typeIndex;

$this->context[] = $this->varStack[$rendering]['variables'];
$this->context[] = array(
'variables' => $variables,
'view' => $view,
);

$html = $this->engine->render($template, $this->varStack[$rendering]['variables']);
$html = $this->engine->render($template, $variables);

array_pop($this->context);
unset($this->varStack[$rendering]);
Expand Down Expand Up @@ -233,44 +261,52 @@ public function renderBlock($name, $variables = array())
throw new FormException(sprintf('This method should only be called while rendering a form element.', $name));
}

$template = $this->lookupTemplate($name);
$context = end($this->context);

$template = $this->lookupTemplate($context['view'], $name);

if (false === $template) {
throw new FormException(sprintf('No block "%s" found while rendering the form.', $name));
}

$variables = array_replace_recursive(end($this->context), $variables);
$variables = array_replace_recursive($context['variables'], $variables);

return $this->engine->render($template, $variables);
}

/**
* Returns the name of the template to use to render the block
*
* @param string $blockName The name of the block
* @param FormView $view The form view
* @param string $blockName The name of the block
*
* @return string|Boolean The template logical name or false when no template is found
*/
protected function lookupTemplate($blockName)
protected function lookupTemplate(FormView $view, $block)
{
if (isset(self::$cache[$blockName])) {
return self::$cache[$blockName];
}

$template = $blockName.'.html.php';
/*
if ($this->templateDir) {
$template = $this->templateDir.':'.$template;
$file = $block.'.html.php';

$themes = $view->hasParent() ? array() : $this->resources;

if ($this->themes->contains($view)) {
$themes = array_merge($themes, $this->themes[$view]);
}
*/
$template = 'FrameworkBundle:Form:'.$template;
if (!$this->engine->exists($template)) {
$template = false;

for ($i = count($themes) - 1; $i >= 0; --$i) {

$template = $themes[$i].':'.$file;

if ($this->engine->exists($template)) {
return $template;
}
}

self::$cache[$blockName] = $template;
if ($view->hasParent()) {
return $this->lookupTemplate($view->getParent(), $block);
}

return $template;
return false;
}

public function getName()
Expand Down
Expand Up @@ -29,9 +29,13 @@ public function __construct($root, $rootCustom)
public function parse($name)
{
$parts = explode(':', $name);
$name = $parts[count($parts)-1];
$name = $parts[count($parts) - 1];

$path = ($name{0} === '_' ? $this->rootCustom : $this->root).'/'.$name;
if ($name[0] == '_') {
$path = $this->rootCustom.'/'.$name;
} else {
$path = $this->root.'/'.$parts[count($parts) - 2].'/'.$name;
}

return new TemplateReference($path, 'php');
}
Expand Down
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;

require_once __DIR__.'/Fixtures/StubTemplateNameParser.php';
require_once __DIR__.'/Fixtures/StubTranslator.php';

use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
use Symfony\Component\Form\FormView;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Tests\Component\Form\AbstractTableLayoutTest;

class FormHelperTableTest extends AbstractTableLayoutTest
{
protected $helper;

protected function setUp()
{
parent::setUp();

$root = realpath(__DIR__.'/../../../Resources/views');
$rootCustom = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootCustom);
$loader = new FilesystemLoader(array());
$engine = new PhpEngine($templateNameParser, $loader);

$this->helper = new FormHelper($engine, array(
'FrameworkBundle:Form',
'FrameworkBundle:FormTable'
));

$engine->setHelpers(array(
$this->helper,
new TranslatorHelper(new StubTranslator()),
));
}

protected function tearDown()
{
$this->helper = null;
}

protected function renderEnctype(FormView $view)
{
return (string)$this->helper->enctype($view);
}

protected function renderLabel(FormView $view, $label = null, array $vars = array())
{
return (string)$this->helper->label($view, $label, $vars);
}

protected function renderErrors(FormView $view)
{
return (string)$this->helper->errors($view);
}

protected function renderWidget(FormView $view, array $vars = array())
{
return (string)$this->helper->widget($view, $vars);
}

protected function renderRow(FormView $view, array $vars = array())
{
return (string)$this->helper->row($view, $vars);
}

protected function renderRest(FormView $view, array $vars = array())
{
return (string)$this->helper->rest($view, $vars);
}
}
Expand Up @@ -32,7 +32,7 @@ protected function setUp()
{
parent::setUp();

$root = realpath(__DIR__.'/../../../Resources/views/Form');
$root = realpath(__DIR__.'/../../../Resources/views');
$rootCustom = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootCustom);
$loader = new FilesystemLoader(array());
Expand Down

0 comments on commit f39ce67

Please sign in to comment.