实例化命名空间类中的小树枝


Instantiating twig in a namespaced class

我在试图找出如何以特定方式使用Twig时遇到了一些问题。我正在尝试编写一个视图类,无论安装了什么模板系统,我都可以在应用程序中使用它,这样我唯一需要更改的就是模板本身和视图类。

然而,当我尝试在类中创建trick对象时,我会得到Fatal error: Class 'Template'Twig_Loader_Filesystem' not found错误,并且我真的不确定我缺少了什么。

有人能给我指正确的方向吗?

以下是我目前所掌握的。。。

composer.json

{
    "name": "Movies",
    "description": "Find movies",
    "require": {
        "ext-curl": "*",
        "twig/twig": "~1.0"
   },
    "autoload": {
        "classmap": [
            "app/classes"
        ]
    }
}

index.php

require_once 'app/app.php';
use Template'Template;
echo Template::render('hello.html', array('name' => 'bob', 'age' => '33'));

app/app.php

define('APP_DIRECTORY', __DIR__ . '/..');
require_once APP_DIRECTORY . '/vendor/autoload.php';

app/classes/Template.class.php

namespace Template;
class Template {
    static function render($template_name, $template_data = array()) {
        $loader = new Twig_Loader_Filesystem(APP_DIRECTORY . '/app/templates');
        $twig = new Twig_Environment($loader);
        return $twig->render('hello.html', array('name' => 'lisa', 'age' => '33'));
    }
}

好吧,事实证明,每当我试图在Template类中包含trick名称空间时,我都做错了。看看我的组织组织组织的其他项目,结果只是添加了

use Twig_Environment;
use Twig_Loader_Filesystem;

去上课就够了。我不确定我是否真的必须添加任何内容,因为Autoloader已经包含在内,或者我是否必须包含部分路径,比如Twig_Twig/Loader/Filesystem或类似的东西。显然这两种方法都不起作用。