[HttpKernel] simplified and enhanced code managing the hinclude strategy

This commit is contained in:
Fabien Potencier 2013-01-04 18:47:22 +01:00
parent 403bb060ce
commit 1f1392dc8b
1 changed files with 11 additions and 24 deletions

View File

@ -32,7 +32,7 @@ class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy
* *
* @param EngineInterface|\Twig_Environment $templating An EngineInterface or a \Twig_Environment instance * @param EngineInterface|\Twig_Environment $templating An EngineInterface or a \Twig_Environment instance
* @param UriSigner $signer A UriSigner instance * @param UriSigner $signer A UriSigner instance
* @param string $globalDefaultTemplate The content of the global default template * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
*/ */
public function __construct($templating, UriSigner $signer = null, $globalDefaultTemplate = null) public function __construct($templating, UriSigner $signer = null, $globalDefaultTemplate = null)
{ {
@ -47,6 +47,10 @@ class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* Additional available options:
*
* * default: The default content (it can be a template name or the content)
*/ */
public function render($uri, Request $request = null, array $options = array()) public function render($uri, Request $request = null, array $options = array())
{ {
@ -58,31 +62,14 @@ class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy
$uri = $this->signer->sign($this->generateProxyUri($uri, $request)); $uri = $this->signer->sign($this->generateProxyUri($uri, $request));
} }
$defaultTemplate = isset($options['default']) ? $options['default'] : null; $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
$defaultContent = null; if ($this->templateExists($template)) {
$content = $this->templating->render($template);
if (null !== $defaultTemplate) {
if ($this->templateExists($defaultTemplate)) {
$defaultContent = $this->templating->render($defaultContent);
} else { } else {
$defaultContent = $defaultTemplate; $content = $template;
}
} elseif ($this->globalDefaultTemplate) {
$defaultContent = $this->templating->render($this->globalDefaultTemplate);
} }
return $this->renderHIncludeTag($uri, $defaultContent); return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $content);
}
/**
* Renders an HInclude tag.
*
* @param string $uri A URI
* @param string $defaultContent Default content
*/
protected function renderHIncludeTag($uri, $defaultContent = null)
{
return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $defaultContent);
} }
private function templateExists($template) private function templateExists($template)