1 回答
TA贡献1891条经验 获得超3个赞
Plates引擎工厂需要一个视图文件夹参数(参见Plates doc):
所以你必须在你的PHP-DI配置文件中添加这个创建:
对于板 V4:
// config.php
return [
// ...
\League\Plates\Engine::class => function(){
return League\Plates\Engine::create('/path/to/templates', 'phtml');
},
];
对于 Plates V3,我会尝试:
// config.php
return [
// ...
\League\Plates\Engine::class => function(){
return new League\Plates\Engine('/path/to/templates');
},
];
或者
// config.php
return [
// ...
\League\Plates\Engine::class => DI\create()
->constructor('/path/to/templates')
,
];
设计说明:
就我个人而言,我不会对模板引擎使用依赖注入,我认为在基本控制器类中实例化 Plates 引擎会更好。
namespace controllers;
use League\Plates\Engine;
abstract class BaseController
{
/**
* @var \League\Plates\Engine
*/
protected $templates;
public function __construct()
{
$this->templates=new Engine(\TEMPLATE_ROOT);
$this->templates->loadExtension(new \League\Plates\Extension\Asset(\APP_ROOT));
}
protected function renderView(string $viewname, array $variables=[])
{
return $this->templates->render($viewname,$variables);
}
}
对于使用Plates以下内容的子控制器:
namespace controllers;
class MyController extends BaseController
{
public function index()
{
return $this->renderView('home');
}
}
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报