Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle][Twig] findAllTemplates not returning all templates #25811

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
nicoschoenmaker opened this issue Jan 16, 2018 · 6 comments
Closed

Comments

@nicoschoenmaker
Copy link

nicoschoenmaker commented Jan 16, 2018

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 4.0.3

TL;DR; Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder doesn't find templates inside the templates/ folder.

This is because the TemplateFinder is unaware of the paths setting in the twig config. Changing the default directory structure in Symfony 4 means this setting is used more often now.

Related issue: #21035
Example usage in the wild: Tracker

Steps to reproduce:

composer create-project symfony/skeleton my_project
cd my_project
composer require templating twig frameworkbundle

Add to config/services.yaml

    App\Controller\MyController:
        arguments:
            $template_finder: "@templating.finder"
        public: true

Add to src/Controller/MyController.php

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder;
use Symfony\Component\HttpFoundation\Response;

class MyController
{
    private $twig;
    private $template_finder;

    public function __construct(\Twig_Environment $environment, TemplateFinder $template_finder)
    {
        $this->twig = $environment;
        $this->template_finder = $template_finder;
    }

    public function listTemplates()
    {
        return new Response(
            $this->twig->render('templates.html.twig', ['templates' => $this->template_finder->findAllTemplates()])
        );
    }
}

Add to config/routes.yaml

index:
    path: /
    controller: App\Controller\MyController::listTemplates

Finally, add to templates/templates.html.twig

{% extends "base.html.twig" %}
{% block body %}
    {% for template in templates %}
        {{ template }}<br />
    {% endfor %}
{% endblock %}

Expected: base.html.twig and templates.html.twig part of the output
Result: Only files inside the FrameworkBundle and TwigBundle found.

@francoispluchino
Copy link
Contributor

I confirm that the system for overwriting bundle templates in the new templates/bundles folder does not work with Symfony 3.4.

You can test with TwigBundle:

If you add your custom template src/Resources/TwigBundle/views/Exception/exception_full.html.twig, the template overriding works properly.

On the other hand, if you add the custom template templates/bundles/TwigBundle/Exception/exception_full.html.twig, the template overriding doesn't works properly. You get in the template cache file var/dev/templates.php:

'TwigBundle:Exception:exception_full.html.twig' => __DIR__.'/../../../vendor/symfony/twig-bundle/Resources/views/Exception/exception_full.html.twig',

and not:

'TwigBundle:Exception:exception_full.html.twig' => __DIR__.'/../../../templates/bundles/TwigBundle/Exception/exception_full.html.twig',

Of course, the Twig config is:

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    paths:
        - "%kernel.project_dir%/templates"

I have a functional solution, but it modifies the files:

  • Symfony\Bundle\FrameworkBundle\Templating\TemplateReference
  • Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder
  • src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml

to detect if it's a overridden template of bundle in the templates/bundles directory.

However, I do not know if it's the best approach, and if it is not better to create a custom file locator extending the Symfony\Component\HttpKernel\Config\FileLocator class, or directly update the Symfony\Component\HttpKernel\Kernel::locateResource() method to include the templates/bundles directory in analysis of resource. If you have a better approach, I'm interested.

What is the approach you want so that I can propose a PR?

@yceruto
Copy link
Member

yceruto commented Sep 24, 2018

I think this was fixed by #27764

@nicoschoenmaker
Copy link
Author

That is actually a great fix. There appears to be some duplicate/similar code within Symfony between the TwigBundle and the FrameworkBundle. I think this bug is in the TemplateFinder of the FrameworkBundle, whilst the PR that you linked fixes stuff in the TwigBundle.

@yceruto
Copy link
Member

yceruto commented Nov 20, 2018

@francoispluchino if you're using the Templating component you should enabling the Twig engine to make it work with the Twig paths and convention:

framework:
    templating:
        engines: ['twig']

@nicoschoenmaker The TemplateFinder only know about paths and conventions for PHP Templating engine. If Twig is installed/enabled the TemplateIterator is used for Twig paths and conventions. That's why I think this issue is already fixed (uses TemplateIterator to find Twig templates)

@francoispluchino
Copy link
Contributor

francoispluchino commented Nov 20, 2018

@yceruto of course, the config is configured correctly :-). As you indicated, your PR #27764 fixes the problem, so since it was merged, the problem no longer exists.

This issue can be closed and marked solved.

@Simperfit
Copy link
Contributor

@fabpot this can be closed

@xabbuh xabbuh closed this as completed Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants