8000 [Framework] Template name is not valid · Issue #12249 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Framework] Template name is not valid #12249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
soullivaneuh opened this issue Oct 18, 2014 · 24 comments
Closed

[Framework] Template name is not valid #12249

soullivaneuh opened this issue Oct 18, 2014 · 24 comments

Comments

@soullivaneuh
Copy link
Contributor

By following the Templates section on Best Practices book, I should have the following code in my controller:

public function indexAction()
{
    return $this->render('default/index.html.twig');
}

This throw the following exception:

Template name "default/index.html.twig" is not valid (format is "bundle:section:template.format.engine").

I'm using "symfony/symfony": "2.6.*".

I think the template path validator should be reviewed, isn't it ?

Thanks.

@sstok
Copy link
Contributor
sstok commented Oct 18, 2014

This (to much my knowledge) only happens when the PHP template engine is enabled, when you have only Twig enabled this works as expected.

@soullivaneuh
Copy link
Contributor Author

My config.yml:

framework:
    templating:
        engines: ['twig']

Seems twig is the only engine enable. Am I right ?

@weaverryan
Copy link
Member

Hi there!

Actually, the problem may just be that the template doesn't exist. Let me show you the error I see when I use the default/index.html.twig format when the template doesn't exist:

screen shot 2014-10-18 at 2 50 12 pm

Notice the 3rd error down does say what you have, even though it really shouldn't say this. It looks like if the template exists, it never gets here - it resolves elsewhere. But if it doesn't exist, then it hits this code, which throws this error.

@soullivaneuh is this what you're seeing? If not, what Symfony version are you seeing?

Regardless, there does seem to be a small "bug" in the error that's being shown here: it should not say that the template format is invalid.

@soullivaneuh
Copy link
Contributor Author

Just updated to the last version of symfony 2.6-beta.

The template, exist and now I have the following error:

Template name "invoice/show.html.twig" is not valid (format is "bundle:section:template.format.engine").

Adding ::, no error.

@javiereguiluz
Copy link
Member

@soullivaneuh could it be a case-sensitive problem? is the template stored in the app/Resources/views/invoice/ directory or is it in app/Resources/views/Invoice/?

@soullivaneuh
Copy link
Contributor Author

The directory is app/Resources/views/invoice/ as suggested on the Best Practices Book.

@weaverryan
Copy link
Member

@soullivaneuh can you post your controller code, the error message and your exact template path again? You originally had $this->render('default/index.html.twig');, but in your last comment, you said the directory is app/Resource/views/invoice. I just did a bunch of playing around (not on 2.6, but I don't think anything significant has changed) and couldn't repeat any issues.

Thanks!

@soullivaneuh
Copy link
Contributor Author

Here an example with a very light controller that just render a view:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 * Class DefaultController
 *
 * @package AppBundle\Controller
 * @Route("/")
 */
class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     * @Method("GET")
     */
    public function indexAction()
    {
        return $this->render('default/index.html.twig');
    }

    /**
     * @Route("/about", name="about")
     * @Method("GET")
     */
    public function aboutAction()
    {
        return $this->render('default/about.html.twig');
    }
}

Here, the views emplacement:

$ ls -l app/Resources/views/default/
total 8
-rw-rw-r-- 1 sullivan sullivan 1434 oct.  21 15:22 about.html.twig
-rw-rw-r-- 1 sullivan sullivan 1297 oct.  31 15:22 index.html.twig

And then, the error:

Template name "default/index.html.twig" is not valid (format is "bundle:section:template.format.engine").
500 Internal Server Error - InvalidArgumentException

If I use ::default/index.html.twig instead of default/index.html.twig, it works.

Here my symfony version I use, by extracting composer.lock file:

"name": "symfony/symfony",
"version": "v2.6.0-BETA1",
"source": {
    "type": "git",
    "url": "https://github.com/symfony/symfony.git",
    "reference": "7bd1d4c0e68e9e2fd9e19c8e5671f71e82a4dc73"
},

@soullivaneuh
Copy link
Contributor Author

Using :default:index.html.twig works also and give me good auto-completion for twig views on PHPStorm. So, what is the good way? :)

@stof
Copy link
Member
stof commented Nov 14, 2014

@soullivaneuh The Symfony2 plugin for PHPStorm is also able to autocomplete native Twig template names, not only the bundle notation (it may not be configured automatically in the project setting if you started using it before the feature was supported, but you can add the paths manually).

Can you do a dump of get_class($this->get('templating')) in your controller, to see which engine is used ? I suspect that several engines are enabled when seeing your issue

@soullivaneuh
Copy link
Contributor Author

Yes, full path works to but throw Exception on Symfony.
Why multiple engines enabled should throws this error? I don't really see the relation.

In a nutshell:

Format Symfony PHPStorm auto-complete
default/index.html.twig Fails Works
::default/index.html.twig Works Fails
:default:index.html.twig Works Works

Results of var_dump(get_class($this->get('templating'))):

string 'Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine' (length=58)

And my framework config extract:

framework:
    templating:
        engines: ['twig']

@stof
Copy link
Member
stof commented Nov 14, 2014

OK, can you do a dump of the engine itself, to see which engines are registered in its $engines private property ? If the DelegatingEngine is used, it means several engines are enabled. Maybe there is another config file enabling more engines

@stof
Copy link
Member
stof commented Nov 14, 2014

Btw, for ::default/index.html.twig failing in PHPStorm autocomplete, you should open an issue on https://github.com/Haehnchen/idea-php-symfony2-plugin/

@soullivaneuh
Copy link
Contributor Author

Indeed, $engines has two elements:

  protected 'engines' => 
    array (size=2)
      0 => 
        object(Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine)[1578]
          protected 'stopwatch' => 
            object(Symfony\Component\Stopwatch\Stopwatch)[388]
              ...
          protected 'locator' => 
            object(Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator)[1581]
              ...
          protected 'environment' => 
            object(Twig_Environment)[1579]
              ...
          protected 'parser' => 
            object(Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser)[1582]
              ...
      1 => 
        object(Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine)[1578]
          protected 'stopwatch' => 
            object(Symfony\Component\Stopwatch\Stopwatch)[388]
              ...
          protected 'locator' => 
            object(Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator)[1581]
              ...
          protected 'environment' => 
            object(Twig_Environment)[1579]
              ...
          protected 'parser' => 
            object(Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser)[1582]
              ...

Seems to be same engines...

I also include assets_version.php:

$container->loadFromExtension('framework', array(
    'templating'      => array(
        'engines'           => array('twig'),
        'assets_version'    => uniqid(),
        'assets_version_format' => '%%s?v=%%s'
    ),
));

Could it be linked?

@soullivaneuh
Copy link
Contributor Author

It was the issue! :)

By commented 'engines' in assets_version.php:

$container->loadFromExtension('framework', array(
    'templating'      => array(
//        'engines'           => array('twig'),
        'assets_version'    => uniqid(),
        'assets_version_format' => '%%s?v=%%s'
    ),
));

I have now only one engine and default/index.html.twig works well.

But the question is, why having two engines cause this error? Not really clear for me...

@stof
Co 8000 py link
Member
stof commented Nov 14, 2014

@soullivaneuh this is because the DelegatingEngine relies on Engine::supports() to choose the engine. But the TwigEngine defined in TwigBundle is not consistent between render() and supports. It supports rendering templates with the Twig native names, but TwigEngine::supports() fails when it does not rely on the bundle notation because of the way it uses to detect the extension

Regarding the engines, we should probably apply array_unique on the list of engines to avoid duplicates when

@soullivaneuh
Copy link
Contributor Author

Ok if I understand well the bug is cause by twice twig service definition.

So if I want later to user twig and php engines I won't have this issue, right?

Thanks for your explanations.

@stof
Copy link
Member
stof commented Nov 14, 2014

@soullivaneuh no, as soon as you enable several engines, you will have the bug seen here.

The reason why it appeared for you is because you enabled twig twice (once in each config file). What I'm saying is that we could be smart enough to avoid registering the same engine multiple times (which is only wasting time in the delegating engine), which will solve it for the case of listing twig several times.
But for the case where you really use several engines, we need to fix the bug itself.

@soullivaneuh
Copy link
Contributor Author

Ok for me, issue solved.

@weaverryan
Copy link
Member

@stof is this something that should actually be fixed? Do we have an issue for this?

@stof
Copy link
Member
stof commented Nov 14, 2014

It is something which should be fixed. I think there was already some discussion related to this bug, but I cannot find the issue and I'm not sure it was in this repo (maybe it happened in the doc repo during a review), so I'm reopening this issue to keep track of it.

@stof stof reopened this Nov 14, 2014
@sstok
Copy link
Contributor
sstok commented Nov 15, 2014

#6919 (original issue)
#7818 (this should have fixed it but was closed by the author)

Documented side-effect.
symfony/symfony-docs#4220

@stof
Copy link
Member
stof commented Nov 15, 2014

@sstok the "original issue" is a totally different topic. The current issue is not related to bundle inheritance at all

@sstok
Copy link
Contributor
sstok commented Nov 15, 2014

Correct, it was related to #8892 as the problem is similar.
When you enable other template engines (like PHP) you can't use the Twig namespace syntax or short-part notation anymore.

PR #7818 proposed a fix this but it was closed

fabpot added a commit that referenced this issue Dec 8, 2014
…he shortcut n... (aitboudad)

This PR was merged into the 2.3 branch.

Discussion
----------

[FrameworkBundle][Template name] avoid  error message for the shortcut n...

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | #12249,  #12254
| Tests pass?   | yes
| License       | MIT

Commits
-------

055129c [FrameworkBundle][Template name] avoid  error message for the shortcut notation.
@fabpot fabpot closed this as completed Dec 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0