Plates Template Engine - 与Twig'pathFor'相同的URI扩展名?

时间:2018-06-01 08:08:08

标签: php twig slim templating plates

我正在通过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扩展名来填充表单的action参数。我正在尝试使用Plates的URI扩展来执行相同的操作:

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

是否有人专门使用此模板引擎和Slim的URI扩展?我错了,它基本上是Twig的pathFor扩展的同义词?我应该放弃并只使用Twig吗?谢谢你的建议。

1 个答案:

答案 0 :(得分:1)

您可以使用环境中的URI

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