ソースを参照

[TwigBundle] converted path and url tags to functions

{% url 'blog_post' with { 'id': post.id } %} -> {{ url('blog_post', { 'id': post.id }) }}
{% path 'blog_post' with { 'id': post.id } %} -> {{ path('blog_post', { 'id': post.id }) }}
Fabien Potencier 14 年 前
コミット
5d65f3edbd

+ 23 - 6
src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php

@@ -59,6 +59,29 @@ class TemplatingExtension extends \Twig_Extension
         );
     }
 
+    /**
+     * Returns a list of global functions to add to the existing list.
+     *
+     * @return array An array of global functions
+     */
+    public function getGlobals()
+    {
+        return array(
+            'fn_url'  => new \Twig_Function($this, 'getUrl'),
+            'fn_path' => new \Twig_Function($this, 'getPath'),
+        );
+    }
+
+    public function getPath($name, array $parameters = array())
+    {
+        return $this->container->get('router')->generate($name, $parameters, false);
+    }
+
+    public function getUrl($name, array $parameters = array())
+    {
+        return $this->container->get('router')->generate($name, $parameters, true);
+    }
+
     /**
      * Returns the token parser instance to add to the existing list.
      *
@@ -88,12 +111,6 @@ class TemplatingExtension extends \Twig_Extension
             // {% flash 'notice' %}
             new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'getFlash'),
 
-            // {% path 'blog_post' with { 'id': post.id } %}
-            new PathTokenParser(),
-
-            // {% url 'blog_post' with { 'id': post.id } %}
-            new UrlTokenParser(),
-
             // {% include 'sometemplate.php' with { 'something' : 'something2' } %}
             new IncludeTokenParser(),
         );

+ 0 - 51
src/Symfony/Bundle/TwigBundle/Node/RouteNode.php

@@ -1,51 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\TwigBundle\Node;
-
-/*
- * 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.
- */
-
-/**
- * 
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class RouteNode extends \Twig_Node
-{
-    public function __construct(\Twig_NodeInterface $route, \Twig_Node_Expression $attributes = null, $absolute, $lineno, $tag = null)
-    {
-        parent::__construct(array('route' => $route, 'route_attributes' => $attributes), array('absolute' => $absolute), $lineno, $tag);
-    }
-
-    /**
-     * Compiles the node to PHP.
-     *
-     * @param \Twig_Compiler A Twig_Compiler instance
-     */
-    public function compile($compiler)
-    {
-        $compiler
-            ->addDebugInfo($this)
-            ->write('echo $this->env->getExtension(\'templating\')->getContainer()->get(\'router\')->generate(')
-            ->subcompile($this->getNode('route'))
-            ->raw(', ')
-        ;
-
-        $attr = $this->getNode('route_attributes');
-        if ($attr) {
-            $compiler->subcompile($attr);
-        } else {
-            $compiler->raw('array()');
-        }
-        $compiler->raw(', ')
-            ->raw($this->getAttribute('absolute') ? 'true' : 'false')
-            ->raw(");")
-        ;
-    }
-}

+ 0 - 62
src/Symfony/Bundle/TwigBundle/TokenParser/PathTokenParser.php

@@ -1,62 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\TwigBundle\TokenParser;
-
-use Symfony\Bundle\TwigBundle\Node\RouteNode;
-
-/*
- * 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.
- */
-
-/**
- * 
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class PathTokenParser extends \Twig_TokenParser
-{
-    /**
-     * Parses a token and returns a node.
-     *
-     * @param  \Twig_Token $token A Twig_Token instance
-     *
-     * @return \Twig_NodeInterface A Twig_NodeInterface instance
-     */
-    public function parse(\Twig_Token $token)
-    {
-        $lineno = $token->getLine();
-        $stream = $this->parser->getStream();
-
-        $route = $this->parser->getExpressionParser()->parseExpression();
-
-        $attributes = null;
-        if ($stream->test('with')) {
-            $stream->next();
-            $attributes = $this->parser->getExpressionParser()->parseExpression();
-        }
-
-        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
-
-        return new RouteNode($route, $attributes, $this->isAbsolute(), $lineno, $this->getTag());
-    }
-
-    protected function isAbsolute()
-    {
-        return false;
-    }
-
-    /**
-     * Gets the tag name associated with this token parser.
-     *
-     * @param string The tag name
-     */
-    public function getTag()
-    {
-        return 'path';
-    }
-}

+ 0 - 37
src/Symfony/Bundle/TwigBundle/TokenParser/UrlTokenParser.php

@@ -1,37 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\TwigBundle\TokenParser;
-
-use Symfony\Bundle\TwigBundle\Node\RouteNode;
-
-/*
- * 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.
- */
-
-/**
- * 
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class UrlTokenParser extends PathTokenParser
-{
-    protected function isAbsolute()
-    {
-        return true;
-    }
-
-    /**
-     * Gets the tag name associated with this token parser.
-     *
-     * @param string The tag name
-     */
-    public function getTag()
-    {
-        return 'url';
-    }
-}

+ 3 - 3
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/admin.twig

@@ -4,12 +4,12 @@
         Admin
     </h3>
 
-    <form action="{% path '_profiler_import' %}" method="post" enctype="multipart/form-data">
+    <form action="{{ path('_profiler_import') }}" method="post" enctype="multipart/form-data">
         <div style="margin-bottom: 10px">
-            &raquo;&nbsp;<a href="{% path '_profiler_purge' with { 'token': token } %}">Purge</a>
+            &raquo;&nbsp;<a href="{{ path('_profiler_purge', { 'token': token }) }}">Purge</a>
         </div>
         <div style="margin-bottom: 10px">
-            &raquo;&nbsp;<a href="{% path '_profiler_export' with { 'token': token } %}">Export</a>
+            &raquo;&nbsp;<a href="{{ path('_profiler_export', { 'token': token }) }}">Export</a>
         </div>
         &raquo;&nbsp;<label for="file">Import</label><br />
         <input type="file" name="file" id="file" /><br />

+ 1 - 1
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.twig

@@ -22,7 +22,7 @@
                         <li
                             {% if name == panel %}class="selected"{% endif %}
                         >
-                            <a href="{% path '_profiler_panel' with { 'token': token, 'panel': name } %}">{{ menu|raw }}</a>
+                            <a href="{{ path('_profiler_panel', { 'token': token, 'panel': name }) }}">{{ menu|raw }}</a>
                         </li>
                     {% endif %}
                 {% endfor %}

+ 1 - 1
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/results.twig

@@ -12,7 +12,7 @@
         </tr>
         {% for elements in tokens %}
             <tr>
-                <td><a href="{% path '_profiler' with { 'token': elements.token } %}">{{ elements.token }}</a></td>
+                <td><a href="{{ path('_profiler', { 'token': elements.token }) }}">{{ elements.token }}</a></td>
                 <td>{{ elements.ip }}</td>
                 <td>{{ elements.url }}</td>
                 <td>{{ elements.time|date('r') }}</td>

+ 1 - 1
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/search.twig

@@ -3,7 +3,7 @@
         <img style="margin: 0 5px 0 0; vertical-align: middle; height: 16px" width="16" height="16" alt="Search" src="{% asset 'bundles/webprofiler/images/search.png' %}" />
         Search
     </h3>
-    <form action="{% path '_profiler_search' %}" method="get">
+    <form action="{{ path('_profiler_search') }}" method="get">
         <label for="ip">IP</label>
         <input type="text" name="ip" id="ip" value="{{ ip }}" />
         <div class="clearfix"></div>