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

时间:2017-07-21 10:30:12

标签: symfony templates twig symfony-2.8

我搜索一个方法来获取Symfony2项目中所有树枝模板的列表。 我希望得到这样的结果:

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

你有什么想法吗?

感谢。

3 个答案:

答案 0 :(得分:6)

您好您可以尝试此代码但您需要调整它

    $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);

答案 1 :(得分:0)

在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;
}

答案 2 :(得分:0)

您可以复制在以下位置找到的getLoaderPaths

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

或仅使用

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

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