Plates 模板引擎 - URI 扩展与 Twig 'pathFor' 相同?

Plates Template Engine - URI extension same as Twig 'pathFor'?

我正在学习有关 Slim 框架的教程。作者使用 Twig,我更喜欢使用 Plates 模板引擎。我已经能够修改所有课程以使用 Plates 模板,直到作者开始使用 baseUrlpathFor 扩展。

我看到 Plates 有一个名为 URI 的扩展,我认为它与 Twig 的 pathFor 同义。

不幸的是,我一辈子都想不出如何启用它。阅读文档,我认为下面的代码可以做到,但到目前为止还没有运气。

require 'vendor/autoload.php';

$app = new Slim\App([
    'settings' => [
        'displayErrorDetails' => true
    ]
]);

$container = $app->getContainer();

$container['view'] = function ($container) {
    $plates = new League\Plates\Engine(__DIR__ . '/templates');
    $plates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));
    return $plates;
};

$app->get('/contact', function($request, $response) {
    return $this->view->render('contact');
});

$app->post('/contact', function($request, $response) {
    return $response->withRedirect('http://slim-local.com/contact/confirm');
})->setName('contact');

$app->get('/contact/confirm', function($request, $response) {
    return $this->view->render('contact_confirm');
});

$app->run();

然后在模板中,作者使用 pathFor 扩展来填充表单的操作参数。我正在尝试使用 Plates 的 URI 扩展来做同样的事情:

<form action="<?=$this->uri('contact')?>" method="post">

有没有人专门用过这个模板引擎和 Slim 的 URI 扩展?我是否误认为它基本上是 Twig 的 pathFor 扩展名的同义词?我应该放弃而只使用 Twig 吗?谢谢你的建议。

您可以使用环境中的URI

修身 3 示例:

$container['view'] = function ($container) {
    $plates = new \League\Plates\Engine(__DIR__ . '/templates');

    $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
    $plates->loadExtension(new \League\Plates\Extension\URI($uri->__toString()));

    return $plates;
};