changed templating name notation

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).
This commit is contained in:
Fabien Potencier 2010-09-28 08:33:33 +02:00
parent 4ac65cebcf
commit a6dc10c31a
21 changed files with 150 additions and 63 deletions

View File

@ -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');
}
}

View File

@ -43,7 +43,7 @@ class ExceptionController extends ContainerAware
}
$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,

View File

@ -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">
@ -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): ?>
@ -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>

View File

@ -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; ?>

View File

@ -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[

View File

@ -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>

View File

@ -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;?>

View File

@ -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; ?>

View File

@ -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;
@ -136,19 +136,18 @@ class Engine extends BaseEngine
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(
@ -157,18 +156,20 @@ class Engine extends BaseEngine
)
);
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);

View File

@ -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');
}
}

View File

@ -41,7 +41,7 @@ class Loader implements \Twig_LoaderInterface
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);
@ -65,7 +65,7 @@ class Loader implements \Twig_LoaderInterface
return (string) $name;
}
list($name, $options) = $this->engine->splitTemplateName($name, array('renderer' => 'twig'));
list($name, $options) = $this->engine->splitTemplateName($name);
return $name.'_'.serialize($options);
}
@ -86,7 +86,7 @@ class Loader implements \Twig_LoaderInterface
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);
}

View File

@ -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,

View File

@ -37,11 +37,11 @@ class ProfilerController extends ContainerAware
$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'),
@ -145,7 +145,7 @@ class ProfilerController extends ContainerAware
$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'),
@ -169,11 +169,11 @@ class ProfilerController extends ContainerAware
}
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)),
@ -195,7 +195,7 @@ class ProfilerController extends ContainerAware
{
$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'),
@ -218,7 +218,7 @@ class ProfilerController extends ContainerAware
$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),
@ -243,7 +243,7 @@ class ProfilerController extends ContainerAware
$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),
@ -293,11 +293,12 @@ class ProfilerController extends ContainerAware
$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;
}
}

View File

@ -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>

View File

@ -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)) ?>

View File

@ -1,4 +1,4 @@
<?php $view->extend('WebProfilerBundle:Profiler:base') ?>
<?php $view->extend('WebProfilerBundle:Profiler:base.php') ?>
<div class="header">
<h1>

View File

@ -12,4 +12,4 @@
<?php echo $view->get('actions')->render('WebProfilerBundle:Profiler:menu', array('token' => $token)) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:admin', array('token' => $token)) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:admin.php', array('token' => $token)) ?>

View File

@ -1,4 +1,4 @@
<?php $view->extend('WebProfilerBundle:Profiler:base') ?>
<?php $view->extend('WebProfilerBundle:Profiler:base.php') ?>
<div class="header">
<h1>

View File

@ -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)) ?>

View File

@ -17,22 +17,22 @@
<h2>Request Cookies</h2>
<?php if (count($data->getRequestCookies()->all())): ?>
<?php echo $view->render('WebProfilerBundle:Profiler:bag', array('bag' => $data->getRequestCookies())) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:bag.php', array('bag' => $data->getRequestCookies())) ?>
<?php else: ?>
<em>No cookies</em>
<?php endif; ?>
<h2>Requests Headers</h2>
<?php echo $view->render('WebProfilerBundle:Profiler:bag', array('bag' => $data->getRequestHeaders())) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:bag.php', array('bag' => $data->getRequestHeaders())) ?>
<h2>Requests Server Parameters</h2>
<?php echo $view->render('WebProfilerBundle:Profiler:bag', array('bag' => $data->getRequestServer())) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:bag.php', array('bag' => $data->getRequestServer())) ?>
<h2>Response Headers</h2>
<?php echo $view->render('WebProfilerBundle:Profiler:bag', array('bag' => $data->getResponseHeaders())) ?>
<?php echo $view->render('WebProfilerBundle:Profiler:bag.php', array('bag' => $data->getResponseHeaders())) ?>
<h2>Response Session Attributes</h2>
@ -59,5 +59,3 @@
</tr>
<?php endforeach; ?>
</table>
<?php //echo $view->render('WebProfilerBundle:Profiler:bag', array('bag' => $data->getSessionAttributes())) ?>

View File

@ -1,4 +1,4 @@
<?php $view->extend('WebProfilerBundle:Profiler:layout') ?>
<?php $view->extend('WebProfilerBundle:Profiler:layout.php') ?>
<h2>Search Results</h2>