Skip to content

Commit

Permalink
[Templating] fix logic regarding template references and many phpdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion authored and fabpot committed Oct 1, 2013
1 parent decaf59 commit 17c3814
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
22 changes: 22 additions & 0 deletions Tests/TwigEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Twig\Tests;

use Symfony\Bridge\Twig\TwigEngine;
use Symfony\Component\Templating\TemplateReference;

class TwigEngineTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -27,20 +28,41 @@ public function testExistsWithNonExistentTemplates()
$engine = $this->getTwig();

$this->assertFalse($engine->exists('foobar'));
$this->assertFalse($engine->exists(new TemplateReference('foorbar')));
}

public function testExistsWithTemplateWithSyntaxErrors()
{
$engine = $this->getTwig();

$this->assertTrue($engine->exists('error'));
$this->assertTrue($engine->exists(new TemplateReference('error')));
}

public function testExists()
{
$engine = $this->getTwig();

$this->assertTrue($engine->exists('index'));
$this->assertTrue($engine->exists(new TemplateReference('index')));
}

public function testRender()
{
$engine = $this->getTwig();

$this->assertSame('foo', $engine->render('index'));
$this->assertSame('foo', $engine->render(new TemplateReference('index')));
}

/**
* @expectedException \Twig_Error_Syntax
*/
public function testRenderWithError()
{
$engine = $this->getTwig();

$engine->render(new TemplateReference('error'));
}

protected function getTwig()
Expand Down
41 changes: 18 additions & 23 deletions TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\StreamingEngineInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;

/**
* This engine knows how to render Twig templates.
Expand All @@ -38,40 +39,33 @@ public function __construct(\Twig_Environment $environment, TemplateNameParserIn
}

/**
* Renders a template.
* {@inheritdoc}
*
* @param mixed $name A template name
* @param array $parameters An array of parameters to pass to the template
* It also supports \Twig_Template as name parameter.
*
* @return string The evaluated template as a string
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
*/
public function render($name, array $parameters = array())
{
return $this->load($name)->render($parameters);
}

/**
* Streams a template.
* {@inheritdoc}
*
* @param mixed $name A template name or a TemplateReferenceInterface instance
* @param array $parameters An array of parameters to pass to the template
* It also supports \Twig_Template as name parameter.
*
* @throws \RuntimeException if the template cannot be rendered
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
*/
public function stream($name, array $parameters = array())
{
$this->load($name)->display($parameters);
}

/**
* Returns true if the template exists.
* {@inheritdoc}
*
* @param mixed $name A template name
*
* @return Boolean true if the template exists, false otherwise
* It also supports \Twig_Template as name parameter.
*/
public function exists($name)
{
Expand All @@ -82,11 +76,13 @@ public function exists($name)
$loader = $this->environment->getLoader();

if ($loader instanceof \Twig_ExistsLoaderInterface) {
return $loader->exists($name);
return $loader->exists((string) $name);
}

try {
$loader->getSource($name);
// cast possible TemplateReferenceInterface to string because the
// EngineInterface supports them but Twig_LoaderInterface does not
$loader->getSource((string) $name);
} catch (\Twig_Error_Loader $e) {
return false;
}
Expand All @@ -95,11 +91,9 @@ public function exists($name)
}

/**
* Returns true if this class is able to render the given template.
*
* @param string $name A template name
* {@inheritdoc}
*
* @return Boolean True if this class supports the given resource, false otherwise
* It also supports \Twig_Template as name parameter.
*/
public function supports($name)
{
Expand All @@ -115,7 +109,8 @@ public function supports($name)
/**
* Loads the given template.
*
* @param mixed $name A template name or an instance of Twig_Template
* @param string|TemplateReferenceInterface|\Twig_Template $name A template name or an instance of
* TemplateReferenceInterface or \Twig_Template
*
* @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
*
Expand All @@ -128,7 +123,7 @@ protected function load($name)
}

try {
return $this->environment->loadTemplate($name);
return $this->environment->loadTemplate((string) $name);
} catch (\Twig_Error_Loader $e) {
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"symfony/stopwatch": "~2.2"
},
"suggest": {
"symfony/form": "",
"symfony/http-kernel": "",
"symfony/routing": "",
"symfony/templating": "",
"symfony/translation": "",
"symfony/yaml": "",
"symfony/security": "",
"symfony/stopwatch": ""
"symfony/form": "For using the FormExtension",
"symfony/http-kernel": "For using the HttpKernelExtension",
"symfony/routing": "For using the RoutingExtension",
"symfony/templating": "For using the TwigEngine",
"symfony/translation": "For using the TranslationExtension",
"symfony/yaml": "For using the YamlExtension",
"symfony/security": "For using the SecurityExtension",
"symfony/stopwatch": "For using the StopwatchExtension"
},
"autoload": {
"psr-0": { "Symfony\\Bridge\\Twig\\": "" }
Expand Down

0 comments on commit 17c3814

Please sign in to comment.