Skip to content

Commit

Permalink
[form] Add SmartyRenderer* classes implemented in Symfony2.1 Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbrandao committed Sep 12, 2012
1 parent aaaafab commit f30acfb
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 128 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/SmartyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('smarty.bootstrap', $config['bootstrap']);

// Form Extension
$loader->load('form.xml');
$container->setParameter('smarty.form.resources', $config['form']['resources']);

// Menu Extension
Expand Down
140 changes: 41 additions & 99 deletions Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\FunctionPlugin;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\ModifierPlugin;
use Smarty_Internal_Template as SmartyTemplate;
use NoiseLabs\Bundle\SmartyBundle\Form\SmartyRendererInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Util\FormUtil;
Expand All @@ -58,51 +58,23 @@
*/
class FormExtension extends AbstractExtension
{
protected $engine;
protected $resources;
protected $functions;
protected $themes;
protected $varStack;
protected $templates;

/**
* Constructor.
*
* @param EngineInterface $engine The templating engine
* @param array $resources An array of theme names
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
* @var \Symfony\Component\Form\FormRendererInterface
*/
public function __construct(EngineInterface $engine, array $resources = array())
{
$this->engine = $engine;
$this->resources = $resources;
$this->varStack = array();
$this->themes = new \SplObjectStorage();
$this->templates = new \SplObjectStorage();
}
protected $renderer;

/**
* Sets a theme for a given view.
*
* @param FormView $view A FormView instance
* @param array $resources An array of resources
* Constructor.
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
* @param SmartyRendererInterface $renderer A SmartyRendererInterface instance
*/
public function setTheme(FormView $view, array $resources)
public function __construct(SmartyRendererInterface $renderer)
{
$this->themes->attach($view, $resources);
$this->templates = new \SplObjectStorage();
$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function getPlugins()
{
Expand All @@ -119,17 +91,23 @@ public function getPlugins()
}

/**
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'form';
}

/**
*/
public function isChoiceGroup($label)
{
return FormUtil::isChoiceGroup($label);
}

/**
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function isChoiceSelected(FormView $view, $choice)
{
Expand All @@ -144,18 +122,15 @@ public function isChoiceSelected(FormView $view, $choice)
* <form action="..." method="post" {form_enctype form=$form}>
*
* @param array $params Attributes passed from the template.
* @param object $template The SmartyTemplate instance.
*
* @return string The html markup
* @param object $template The \Smarty_Internal_Template instance.
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
* @return string The HTML markup
*/
public function renderEnctype($params, SmartyTemplate $template)
public function renderEnctype($params, \Smarty_Internal_Template $template)
{
$view = $this->extractFunctionParameters($params);
list($view, $parameters) = $this->extractFunctionParameters($params);

return $this->render(reset($view), $template, 'enctype');
return $this->renderer->searchAndRenderBlock($view, 'enctype');
}

/**
Expand All @@ -165,11 +140,8 @@ public function renderEnctype($params, SmartyTemplate $template)
* @param array $variables An array of variables
*
* @return string The html markup
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function renderRest($params, SmartyTemplate $template)
public function renderRest($params, \Smarty_Internal_Template $template)
{
list($view, $variables) = $this->extractFunctionParameters($params);

Expand All @@ -180,14 +152,11 @@ public function renderRest($params, SmartyTemplate $template)
* Renders views which have not already been rendered.
*
* @param array $params Attributes passed from the template.
* @param object $template The SmartyTemplate instance.
* @param object $template The \Smarty_Internal_Template instance.
*
* @return string The html markup
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function renderRow($params, SmartyTemplate $template)
public function renderRow($params, \Smarty_Internal_Template $template)
{
list($view, $variables) = $this->extractFunctionParameters($params);

Expand All @@ -208,14 +177,11 @@ public function renderRow($params, SmartyTemplate $template)
* {$form|form_widget:array('separator' => '+++++')}
*
* @param array $params Attributes passed from the template.
* @param object $template The SmartyTemplate instance.
* @param object $template The \Smarty_Internal_Template instance.
*
* @return string The html markup
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function renderWidget($params, SmartyTemplate $template)
public function renderWidget($params, \Smarty_Internal_Template $template)
{
list($view, $variables) = $this->extractFunctionParameters($params);

Expand All @@ -226,14 +192,11 @@ public function renderWidget($params, SmartyTemplate $template)
* Renders the errors of the given view.
*
* @param array $params Attributes passed from the template.
* @param object $template The SmartyTemplate instance.
* @param object $template The \Smarty_Internal_Template instance.
*
* @return string The html markup
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function renderErrors($params, SmartyTemplate $template)
public function renderErrors($params, \Smarty_Internal_Template $template)
{
list($view) = $this->extractFunctionParameters($params);

Expand All @@ -244,14 +207,11 @@ public function renderErrors($params, SmartyTemplate $template)
* Renders the label of the given view.
*
* @param array $params Attributes passed from the template.
* @param object $template The SmartyTemplate instance.
* @param object $template The \Smarty_Internal_Template instance.
*
* @return string The html markup
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function renderLabel($params, SmartyTemplate $template)
public function renderLabel($params, \Smarty_Internal_Template $template)
{
list($view, $variables) = $this->extractFunctionParameters($params);

Expand All @@ -276,7 +236,7 @@ public function renderLabel($params, SmartyTemplate $template)
*
* @throws FormException if no template block exists to render the given section of the view
*/
protected function render(FormView $view, SmartyTemplate $template, $section, array $variables = array())
protected function render(FormView $view, \Smarty_Internal_Template $template, $section, array $variables = array())
{
$mainTemplate = in_array($section, array('widget', 'row'));
if ($mainTemplate && $view->isRendered()) {
Expand Down Expand Up @@ -335,31 +295,19 @@ protected function render(FormView $view, SmartyTemplate $template, $section, ar
));
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
public function getName()
{
return 'form';
}

/**
* Returns, if available, the $form parameter from the parameters array
* passed to the Smarty plugin function. When missing a FormException is
* thrown.
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*/
protected function extractFunctionParameters(array $parameters)
{
if (!isset($parameters['form'])) {
throw new FormException("'form' parameter missing in Smarty template function.");
throw new FormException('"form" parameter missing in Smarty template function.');
}

if (!$parameters['form'] instanceof FormView) {
throw new \InvalidArgumentException('"form" parameter must be an instance of Symfony\Component\Form\FormView');
}

$view = $parameters['form'];
Expand All @@ -369,15 +317,12 @@ protected function extractFunctionParameters(array $parameters)
}

/**
* Creates SmartyTemplate instances for every resource (template filename)
* Creates \Smarty_Internal_Template instances for every resource (template filename)
* set.
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*
* @return \SplObjectStorage Collection of SmartyTemplate instances
* @return \SplObjectStorage Collection of \Smarty_Internal_Template instances
*/
protected function loadTemplates(FormView $view, SmartyTemplate $template = null)
protected function loadTemplates(FormView $view, \Smarty_Internal_Template $template = null)
{
$resources = $this->resources;

Expand Down Expand Up @@ -406,12 +351,9 @@ protected function loadTemplates(FormView $view, SmartyTemplate $template = null
* be accessible due to be outside template blocks for instance (in a child
* template).
*
* @since 0.2.0
* @author Vítor Brandão <[email protected]>
*
* @param string $block The name of the function
*
* @return SmartyTemplate\false Return the SmartyTemplate where the function
* @return \Smarty_Internal_Template\false Return the \Smarty_Internal_Template where the function
* is found or false if it doesn't exist in any loaded template object.
*/
protected function lookupTemplateFunction($function)
Expand Down
58 changes: 58 additions & 0 deletions Form/SmartyRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This file is part of NoiseLabs-SmartyBundle
*
* NoiseLabs-SmartyBundle is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* NoiseLabs-SmartyBundle is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with NoiseLabs-SmartyBundle; if not, see
* <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2012 Vítor Brandão
*
* @category NoiseLabs
* @package SmartyBundle
* @author Vítor Brandão <[email protected]>
* @copyright (C) 2011-2012 Vítor Brandão <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL-3
* @link http://www.noiselabs.org
* @since 0.2.0
*/

namespace NoiseLabs\Bundle\SmartyBundle\Form;

use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;

/**
* Renders a Symfony2 form in a Smarty template.
*
* @author Vítor Brandão <[email protected]>
*/

class SmartyRenderer extends FormRenderer implements SmartyRendererInterface
{
/**
* @var SmartyRendererEngineInterface
*/
private $engine;

/**
* @param \NoiseLabs\Bundle\SmartyBundle\Form\SmartyRendererEngineInterface $engine
* @param \Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface|null $csrfProvider
*/
public function __construct(SmartyRendererEngineInterface $engine, CsrfProviderInterface $csrfProvider = null)
{
parent::__construct($engine, $csrfProvider);

$this->engine = $engine;
}
}
Loading

0 comments on commit f30acfb

Please sign in to comment.