Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Better coding practise for including Views?
  • Hello,

    I would like to know, what is the better coding practise e.g. for including Views etc.

    Actually I do the following:

    \Theme::instance()->get_template()->set('content', \Theme::instance()->view('admin/types/index', $data));

    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.

    $someview = \View::forge('admin/views/someview', array('count_types' => $this->count_types));

    Will that be accessable with $someview within an other view?

    Thanks very much for your help
    Kay

  • HarroHarro
    Accepted Answer
    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.
  • Thanks Harro for the explanations.

    When I change to this, the "admin"index" view is not loaded for the Partial "content":

            \Theme::instance()->get_partial('subnavigation', 'partials/subnavigation')
                ->set('mdtype', \Functions::valueByIDModelKey(\Session::get('sel_mdtype') ?: $this->first_type, 'Masterdata\Model_Type', 'name'));    
            \Theme::instance()->set_partial('content', 'admin/index')
                ->set($data);

    With the former call, this was working:
    Theme::instance()->get_template()->set('content', \Theme::instance()->view('admin/index'));

    The Partial "subnavigation" is loaded correctly.

    Any Idea?

    Thanks
    Kay
  • I got it :-)

    Used $content in my Template instead of $partials['content']. So I changed this now.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion