在Twig中动态加载模板目录


Load template directory dynamic in Twig

我在我的项目中使用Twig,我不使用Symfony作为PHP框架。

这是我的构造函数:
    $this->_template_dir []= '/views/';
    $this->_template_dir []= 'modules/comments/views/';
    $loader = new Twig_Loader_Filesystem($this->_template_dir);
    $this->_twig = new Twig_Environment($loader, array(
            'cache' => $this->_cache_dir,
            'debug' => TRUE,
            'auto_escape' => FALSE,
            'autoescape' => FALSE,
            'auto_reload' => ! (ENVIRONMENT == 'production')
    ));

是否有一种方法可以在构造函数之后设置模板dir ?像一个方法:

$this->_twig->addTemplateDir('new_folder');

我在文档中没有找到任何东西

你应该能够使用

$this->_twig->setLoader($this->_template_dir[0])

Twig_Loader_Filesystem有一个方法addPath()