获取所有树枝模板的列表 Symfony

Get list of all twig templates Symfony

我正在寻找一种方法来获取我的 Symfony2 项目中所有 twig 模板的列表。 我希望得到这样的结果:

- AcmeBundle:Default:list.html.twig 
- OtherBundle:ParentFolder:new.html.twig

你有什么想法吗?

谢谢。

您好,您可以试试这个代码,但您需要对其进行调整

    $kernel = $this->container->get('kernel');
    $bundles = $kernel->getBundles();
    $res = array();
    foreach ($bundles as $key => $value) {
        $path = $kernel->locateResource("@$key");
        $array = explode('\', $path);
        if (in_array('vendor', $array))
            continue;
        $finder = new Finder();
        $finder->in($path);
        $finder->files()->name('*.twig');
        foreach ($finder as $val) {
            $explodurl = explode('Resources\views\', $val->getPathname());
            $string = end($explodurl);
            $string = str_replace("\", ':', $string);
            $res[] = "$key:$string";
        }
    }
     dump($res);

在 unix ( nginx ) 中使用它

 $res = array();
    foreach ($this->getBundles() as $key => $value) {
        $keyAlias = $value->getName();
        $path = $kernel->locateResource("@$keyAlias");
        $array = explode('//', $path);
        if (in_array('vendor', $array))
            continue;
        $finder = new Finder();
        $finder->in($path);
        $finder->files()->name('*.twig');
        foreach ($finder as $val) {
            $explodurl = explode('//Resources/views/', $val->getPathname());
            $string = end($explodurl);
            $string = str_replace("/", ':', $string);
            $res[] = "$keyAlias:$string";
        }
    }
   dump($res);

private function getBundles() {
    $kernel = $this->getContainer()->get('kernel');
    $bundles = [];
    $allBundles = $kernel->getBundles();
    foreach ($allBundles as $bundle) {
        $reflection = new ReflectionClass($bundle);
        $bundleDirectory = dirname($reflection->getFileName());
        if (!preg_match("/vendor/i", $bundleDirectory, $matches)) {
            $bundles[] = $bundle;
        }
    }
    return $bundles;
}

您可以复制在

找到的getLoaderPaths

https://github.com/symfony/twig-bridge/blob/master/Command/DebugCommand.php

或者直接使用

$loader = $this->twig->getLoader();

因为加载程序将包含路径列表