-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form] Add SmartyRenderer* classes implemented in Symfony2.1 Forms
- Loading branch information
1 parent
aaaafab
commit f30acfb
Showing
9 changed files
with
289 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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'); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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()) { | ||
|
@@ -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']; | ||
|
@@ -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; | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.