Skip to content

Commit

Permalink
changed templating name notation
Browse files Browse the repository at this point in the history
Old notation: bundle:section:name.format:renderer (where both format and renderer are optional)
New notation: bundle:section:name.format.renderer (where only format is optional)

Valid new template names: Blog:Post:index.php, Blog:Post:index.xml.php

The new notation is more explicit and put all templating engines on the same level (there is no
more the concept of a "default" templating engine).

Even if the notation changed, the semantic has not. So, the logical template name for the above
examples is still 'index'. So, if you use a database loader for instance, the template
name is 'index' and everything else are options.

Upgrading current applications can be easily done by appending .php to each existing template
name reference (in both controllers and templates), and changing :twig to .twig for Twig templates
(for twig templates, you should also add .twig within templates themselves when referencing
another Twig templates).
  • Loading branch information
fabpot committed Sep 28, 2010
1 parent 4ac65ce commit a6dc10c
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 63 deletions.
Expand Up @@ -28,6 +28,6 @@ class DefaultController extends ContainerAware
*/
public function indexAction()
{
return $this->container->get('templating')->renderResponse('FrameworkBundle:Default:index');
return $this->container->get('templating')->renderResponse('FrameworkBundle:Default:index.php');
}
}
Expand Up @@ -43,7 +43,7 @@ public function exceptionAction(FlattenException $exception, DebugLoggerInterfac
}

$response = $this->container->get('templating')->renderResponse(
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception' : 'error'),
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'),
array(
'exception' => new SafeDecorator($exception),
'logger' => $logger,
Expand Down
@@ -1,5 +1,5 @@
<?php if (!$embedded): ?>
<?php $view->extend('FrameworkBundle:Exception:layout') ?>
<?php $view->extend('FrameworkBundle:Exception:layout.php') ?>
<?php endif; ?>

<div class="sf-exceptionreset">
Expand Down Expand Up @@ -27,10 +27,10 @@
<div style="clear: both"></div>
</div>

<?php echo $view->render('FrameworkBundle:Exception:traces', array('exception' => $exception, 'position' => 0, 'count' => $previousCount)) ?>
<?php echo $view->render('FrameworkBundle:Exception:traces.php', array('exception' => $exception, 'position' => 0, 'count' => $previousCount)) ?>

<?php foreach ($exception->getPreviouses() as $i => $previous): ?>
<?php echo $view->render('FrameworkBundle:Exception:traces', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?>
<?php echo $view->render('FrameworkBundle:Exception:traces.php', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?>
<?php endforeach; ?>

<?php if (null !== $logger): ?>
Expand All @@ -43,7 +43,7 @@
</h3>

<div id="logs" style="display: none">
<?php echo $view->render('FrameworkBundle:Exception:logs', array('logs' => $logger->getLogs())) ?>
<?php echo $view->render('FrameworkBundle:Exception:logs.php', array('logs' => $logger->getLogs())) ?>
</div>

</div>
Expand Down
@@ -1,12 +1,12 @@
<?php echo sprintf('<?xml version="1.0" encoding="%s" ?>', $view->getCharset())."\n" ?>
<error code="<?php echo $exception->getStatusCode() ?>" message="<?php echo $exception->getStatusText() ?>">
<exception class="<?php echo $exception->getClass() ?>" message="<?php echo $exception->getMessage() ?>">
<?php echo $view->render('FrameworkBundle:Exception:traces', array('exception' => $exception, 'position' => 0, 'count' => ($previousCount = count($exception->getPreviouses())))) ?>
<?php echo $view->render('FrameworkBundle:Exception:traces.php', array('exception' => $exception, 'position' => 0, 'count' => ($previousCount = count($exception->getPreviouses())))) ?>
</exception>
<?php if ($previousCount): ?>
<?php foreach ($exception->getPreviouses() as $i => $previous): ?>
<exception class="<?php echo $previous->getClass() ?>" message="<?php echo $previous->getMessage() ?>">
<?php echo $view->render('FrameworkBundle:Exception:traces', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?>
<?php echo $view->render('FrameworkBundle:Exception:traces.php', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?>
</exception>
<?php endforeach; ?>
<?php endif; ?>
Expand Down
Expand Up @@ -8,7 +8,7 @@
body { font: 11px Verdana, Arial, sans-serif; color: #333 }
.sf-exceptionreset, .sf-exceptionreset .block, .sf-exceptionreset #message { margin: auto }

<?php echo $view->render('FrameworkBundle:Exception:styles') ?>
<?php echo $view->render('FrameworkBundle:Exception:styles.php') ?>
</style>
<script type="text/javascript">
//<![CDATA[
Expand Down
Expand Up @@ -13,7 +13,7 @@
<ol class="traces" id="traces_<?php echo $position ?>" style="display: <?php echo 0 === $position ? 'block' : 'none' ?>">
<?php foreach ($exception->getTrace() as $i => $trace): ?>
<li>
<?php echo $view->render('FrameworkBundle:Exception:trace', array('prefix' => $position, 'i' => $i, 'trace' => $trace)) ?>
<?php echo $view->render('FrameworkBundle:Exception:trace.php', array('prefix' => $position, 'i' => $i, 'trace' => $trace)) ?>
</li>
<?php endforeach; ?>
</ol>
Expand Down
@@ -1,6 +1,6 @@
<?php if (count($exception->getTrace())): ?>
<?php foreach ($exception->getTrace() as $i => $trace): ?>
<?php echo $view->render('FrameworkBundle:Exception:trace.txt', array('i' => $i, 'trace' => $trace)) ?>
<?php echo $view->render('FrameworkBundle:Exception:trace.txt.php', array('i' => $i, 'trace' => $trace)) ?>

<?php endforeach; ?>
<?php endif;?>
@@ -1,7 +1,7 @@
<traces>
<?php foreach ($exception->getTrace() as $i => $trace): ?>
<trace>
<?php echo $view->render('FrameworkBundle:Exception:trace.txt', array('i' => $i, 'trace' => $trace)) ?>
<?php echo $view->render('FrameworkBundle:Exception:trace.txt.php', array('i' => $i, 'trace' => $trace)) ?>

</trace>
<?php endforeach; ?>
Expand Down
33 changes: 17 additions & 16 deletions src/Symfony/Bundle/FrameworkBundle/Templating/Engine.php
Expand Up @@ -37,7 +37,7 @@ class Engine extends BaseEngine
* @param array $renderers An array of renderer instances
* @param mixed $escaper The escaper to use (or false to disable escaping)
*/
public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers = array(), $escaper)
public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers = array(), $escaper = false)
{
$this->level = 0;
$this->container = $container;
Expand Down Expand Up @@ -136,19 +136,18 @@ protected function escapeParameters(array $parameters)
return $parameters;
}

// Bundle:controller:action(.format)(:renderer)
// parses template names following the following pattern:
// bundle:controller:action(.format)(.renderer)
public function splitTemplateName($name, array $defaults = array())
{
$parts = explode(':', $name, 4);

if (sizeof($parts) < 3) {
$parts = explode(':', $name);
if (3 !== count($parts)) {
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
}

$options = array_replace(
array(
'renderer' => 'php',
'format' => '',
'format' => '',
),
$defaults,
array(
Expand All @@ -157,18 +156,20 @@ public function splitTemplateName($name, array $defaults = array())
)
);

if (false !== $pos = strpos($parts[2], '.')) {
$options['format'] = substr($parts[2], $pos);
$parts[2] = substr($parts[2], 0, $pos);
} else {
$format = $this->container->getRequestService()->getRequestFormat();
$elements = explode('.', $parts[2]);
if (3 === count($elements)) {
$parts[2] = $elements[0];
$options['format'] = $elements[1];
$options['renderer'] = $elements[2];
} elseif (2 === count($elements)) {
$parts[2] = $elements[0];
$options['renderer'] = $elements[1];
$format = $this->container->get('request')->getRequestFormat();
if (null !== $format && 'html' !== $format) {
$options['format'] = '.'.$format;
}
}

if (isset($parts[3]) && $parts[3]) {
$options['renderer'] = $parts[3];
} else {
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
}

return array($parts[2], $options);
Expand Down
87 changes: 87 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Templating/EngineTest.php
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.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;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Templating\Engine;

class EngineTest extends TestCase
{
/**
* @dataProvider getSplitTemplateNameTests
*/
public function testSplitTemplateName($name, $parameters)
{
$engine = new Engine($this->getContainerMock(), $this->getLoaderMock());

$this->assertEquals($parameters, $engine->splitTemplateName($name));
}

public function getSplitTemplateNameTests()
{
return array(
array('BlogBundle:Post:index.php', array('index', array('bundle' => 'BlogBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => ''))),
array('BlogBundle:Post:index.twig', array('index', array('bundle' => 'BlogBundle', 'controller' => 'Post', 'renderer' => 'twig', 'format' => ''))),
array('BlogBundle:Post:index.xml.php', array('index', array('bundle' => 'BlogBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'xml'))),
);
}

/**
* @dataProvider getSplitTemplateNameInvalidTests
* @expectedException \InvalidArgumentException
*/
public function testSplitTemplateNameInvalid($name)
{
$engine = new Engine($this->getContainerMock(), $this->getLoaderMock());

$engine->splitTemplateName($name);
}

public function getSplitTemplateNameInvalidTests()
{
return array(
array('BlogBundle:Post:index'),
array('BlogBundle:Post'),
array('BlogBundle:Post:foo:bar'),
array('BlogBundle:Post:index.foo.bar.foobar'),
);
}

protected function getContainerMock()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request
->expects($this->any())
->method('getRequestFormat')
->will($this->returnValue('html'))
;

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$container
->expects($this->exactly(2))
->method('findTaggedServiceIds')
->will($this->returnValue(array()))
;
$container
->expects($this->any())
->method('get')
->will($this->returnValue($request))
;

return $container;
}

protected function getLoaderMock()
{
return $this->getMock('Symfony\Component\Templating\Loader\LoaderInterface');
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Bundle/TwigBundle/Loader/Loader.php
Expand Up @@ -41,7 +41,7 @@ public function getSource($name)
return $name->getContent();
}

list($name, $options) = $this->engine->splitTemplateName($name, array('renderer' => 'twig'));
list($name, $options) = $this->engine->splitTemplateName($name);

$template = $this->engine->getLoader()->load($name, $options);

Expand All @@ -65,7 +65,7 @@ public function getCacheKey($name)
return (string) $name;
}

list($name, $options) = $this->engine->splitTemplateName($name, array('renderer' => 'twig'));
list($name, $options) = $this->engine->splitTemplateName($name);

return $name.'_'.serialize($options);
}
Expand All @@ -86,7 +86,7 @@ public function isFresh($name, $time)
return false;
}

list($name, $options) = $this->engine->splitTemplateName($name, array('renderer' => 'twig'));
list($name, $options) = $this->engine->splitTemplateName($name);

return $this->engine->getLoader()->isFresh($name, $options, $time);
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ class ExceptionController extends ContainerAware
public function showAction(FlattenException $exception, $format)
{
return $this->container->get('templating')->renderResponse(
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception' : 'error'),
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'),
array(
'exception' => new SafeDecorator($exception),
'logger' => null,
Expand Down
Expand Up @@ -37,11 +37,11 @@ public function indexAction($token)
$profiler = $this->container->get('profiler')->loadFromToken($token);

if ($profiler->isEmpty()) {
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound.php', array(
'token' => $token,
));
} else {
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:index', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:index.php', array(
'token' => $token,
'profiler' => new SafeDecorator($profiler),
'collector' => $profiler->get('request'),
Expand Down Expand Up @@ -145,7 +145,7 @@ public function toolbarAction($token = null, $position = null)
$position = false === strpos($this->container->get('request')->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
}

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.php', array(
'position' => $position,
'profiler' => new SafeDecorator($profiler),
'templates' => $this->getTemplates($profiler, '_bar'),
Expand All @@ -169,11 +169,11 @@ public function panelAction($token, $panel)
}

if ($profiler->isEmpty()) {
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound.php', array(
'token' => $token,
));
} else {
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:panel', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:panel.php', array(
'token' => $token,
'profiler' => new SafeDecorator($profiler),
'collector' => new SafeDecorator($profiler->get($panel)),
Expand All @@ -195,7 +195,7 @@ public function listAction($token, $panel)
{
$profiler = $this->container->get('profiler')->loadFromToken($token);

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:menu', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:menu.php', array(
'token' => $token,
'profiler' => new SafeDecorator($profiler),
'templates' => $this->getTemplates($profiler, '_menu'),
Expand All @@ -218,7 +218,7 @@ public function menuAction($token)
$url = $session->get('_profiler_search_url');
$limit = $session->get('_profiler_search_limit');

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.php', array(
'token' => $token,
'profiler' => new SafeDecorator($profiler),
'tokens' => $profiler->find($ip, $url, 10),
Expand All @@ -243,7 +243,7 @@ public function searchResultsAction($token)
$url = $session->get('_profiler_search_url');
$limit = $session->get('_profiler_search_limit');

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results', array(
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.php', array(
'token' => $token,
'profiler' => new SafeDecorator($this->container->get('profiler')->loadFromToken($token)),
'tokens' => $profiler->find($ip, $url, 10),
Expand Down Expand Up @@ -293,11 +293,12 @@ protected function getTemplates($profiler, $suffix)
$templates = array();
foreach ($this->container->getParameter('data_collector.templates') as $name => $template) {
if ($profiler->has($name)) {
if (!$this->container->get('templating')->exists($template.$suffix)) {
$tpl = preg_replace('/\.(.+?)$/', $suffix.'.$1', $template);
if (!$this->container->get('templating')->exists($tpl)) {
continue;
}

$templates[$name] = $template.$suffix;
$templates[$name] = $tpl;
}
}

Expand Down
Expand Up @@ -6,15 +6,15 @@

<parameters>
<parameter key="data_collector.templates" type="collection">
<parameter key="config">WebProfilerBundle:Profiler:config</parameter>
<parameter key="request">WebProfilerBundle:Profiler:request</parameter>
<parameter key="exception">WebProfilerBundle:Profiler:exception</parameter>
<parameter key="events">WebProfilerBundle:Profiler:events</parameter>
<parameter key="logger">WebProfilerBundle:Profiler:logger</parameter>
<parameter key="timer">WebProfilerBundle:Profiler:timer</parameter>
<parameter key="memory">WebProfilerBundle:Profiler:memory</parameter>
<parameter key="db">DoctrineBundle:Profiler:db</parameter>
<parameter key="mongodb">DoctrineMongoDBBundle:Profiler:mongodb</parameter>
<parameter key="config">WebProfilerBundle:Profiler:config.php</parameter>
<parameter key="request">WebProfilerBundle:Profiler:request.php</parameter>
<parameter key="exception">WebProfilerBundle:Profiler:exception.php</parameter>
<parameter key="events">WebProfilerBundle:Profiler:events.php</parameter>
<parameter key="logger">WebProfilerBundle:Profiler:logger.php</parameter>
<parameter key="timer">WebProfilerBundle:Profiler:timer.php</parameter>
<parameter key="memory">WebProfilerBundle:Profiler:memory.php</parameter>
<parameter key="db">DoctrineBundle:Profiler:db.php</parameter>
<parameter key="mongodb">DoctrineMongoDBBundle:Profiler:mongodb.php</parameter>
</parameter>
</parameters>
</container>
@@ -1,3 +1,3 @@
<?php $view->extend('WebProfilerBundle:Profiler:layout') ?>
<?php $view->extend('WebProfilerBundle:Profiler:layout.php') ?>

<?php echo $view->render($template, array('data' => $collector)) ?>
@@ -1,4 +1,4 @@
<?php $view->extend('WebProfilerBundle:Profiler:base') ?>
<?php $view->extend('WebProfilerBundle:Profiler:base.php') ?>

<div class="header">
<h1>
Expand Down

0 comments on commit a6dc10c

Please sign in to comment.