0

我想在 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 变量来使用 Blade 模板,而是简单地调用 View()。如果可能的话,我想实现这样的目标。我标记了 laravel,以便任何对 Laravel 有更多经验的人都可以更好地理解如何实现它?

你有什么建议?

PS:我正在使用 composer autoload 来加载所有文件。

4

1 回答 1

0

恐怕我真的只是一个 CI3 用户,但已经阅读了一些关于 CI4 的内容,并且仍然有一个您可以使用的基本控制器。你应该看看https://codeigniter4.github.io/userguide/extending/basecontroller.html?highlight=basecontroller。然后,您可以通过所有扩展控制器使您的 $this->templates 可用。

于 2019-07-12T14:26:19.730 回答