In the documentatiobn I mostly find something like: $theme = \Theme::instance(); $theme->get_template()->set('content', $theme->view('admin/types/index', $data));
What is the better way of doing it?
Will using "\Theme::instance();" multiple times produce more traffic or overhead?
If I e.g. do "$theme = \Theme::instance();" in the first before() of a module, then I cound not access $theme in the next level, so I need to instance Theme again and again, which make no sense for me.
In this context: what is \View::forge for? Could this be used to include a complete pre-defined View into an existing View without using Partials? e.g.
When you use the Theme class, you should not use set() on the page template for anything other then template variables, but use set_partial() to set client views on the template.
I use this system:
* in before() of my base controller:
// define the theme template to use for this page \Theme::instance()->set_template($this->template);
// set global template data: page breadcrumbs \Theme::instance()->get_template()->set('breadcrumbs', $breadcrumbs);
// set global template data: user info partial $this->user = Auth::instance(); \Theme::instance()->set_partial('user_info', 'global/partials/userinfo')->set('user', $this->user, false); \Theme::instance()->get_template()->set('user', $this->user, false);
* in before of my app controller:
// title and header for all actions of this controller \Theme::instance()->set_partial('header', 'global/partials/header') ->set('icon', 'users') ->set('title', __('groups.title')) ->set('subtitle', __('groups.subtitle'));
* in the action
// set the template body partial \Theme::instance()->set_partial('content', 'groups/index') ->set('authorisations', $this->authorisations, false) ->set($data);
If configured the Theme class will automatically prefix the view name with the name of the module, in case you're in a module controller.
There isn't much overhead in calling instance(), but if you don't want that, you can fetch it in before(). But you need to assign it to a class property ($this->theme), not to a local variable ($theme), of you want it to be available in other methods too.
The View class isn't theme aware, so View::forge() will load the view file from the 'views' folder, not from your theme. $someview here is a variable in the local scope of the method, so it won't be accessable elsewhere unless you pass it on.