我想在Codeigniter-4项目中使用模板。
如果我的控制器看起来像这样
class Blog extends \CodeIgniter\Controller
{
public function index()
{
$data = [
'todo_list' => ['Clean House', 'Call Mom', 'Run Errands'],
'title' => "My Real Title",
'heading' => "My Real Heading"
];
echo view('blogview', $data);
}
}我想使用一个模板系统,它的工作原理如下:
// Create new Plates instance
$templates = League\Plates\Engine::create('/path/to/templates');
// Render a template with the given data
echo $templates->render('profile', ['name' => 'Jonathan']);我的问题是-实例化$templates对象的最佳位置在哪里?
我可以在我知道是不好的练习的每种方法中重复它。……或者..。我可以在__contstructor()中执行此操作,并将其赋值给$this->templates,然后对要使用该模板的每个控制器执行此操作。我觉得还有更好的方法。
我几乎没有使用Laravel的经验,我没有专门设置这个$templates变量来使用刀片模板,而只是简单地调用View()。如果可能的话,我想实现这样的目标。我标记了laravel,这样任何有更多Laravel经验的人都可以更好地理解如何实现它?
你有什么建议?
PS:我正在使用composer自动加载来加载所有的文件。
发布于 2019-07-12 22:26:20
恐怕我真的只是一个CI3用户,但我已经读了一点关于CI4的知识,仍然有一个基本控制器可以使用。你应该看看https://codeigniter4.github.io/userguide/extending/basecontroller.html?highlight=basecontroller。然后,您可以通过所有扩展控制器使$this->模板可用。
https://stackoverflow.com/questions/49423858
复制相似问题