Sidebar partials for multilevel user in Codeigniter Sidebar partials for multilevel user in Codeigniter codeigniter codeigniter

Sidebar partials for multilevel user in Codeigniter


Hi @Goldplate it is good to keep it at the view. Although both ways works, keeping it in the view makes it easy to change filenames when you need to.

You can create a helper function to keep things simple, for example:

  1. Create a helper with the name template_helper.php in the helpers directory.
  2. Add the code below.

    <?phpdefined('BASEPATH') or exit('No direct script access allowed');function load_partial($view_path, $view_data = null){    $ci =& get_instance(); //get_instance is an instance of codeigniter object    if (strstr($view_path, '.')) {        $view_path = str_replace('.', '/', $view_path);    }    $ci->load->view($view_path, $view_data);}

And you can use it like this:

<?php if ($this->session->userdata('userLevel') == 1) {?>     load_partial('sidebars.level_one');<?php } else if($this->session->userdata('userLevel') == 2) {?>    load_partial('sidebars.level_two');<?php } ?>

So here "sidebars" is a directory that contains files level_one.php and level_two.php

If you see a dot in 'sidebars.level_one' it is just replacing a forward slash '/' and for easy access.

This is just to give you an idea. Let me know if it helps.


Both the process is good. It up to you to choose yours.

As it is templating, I like to keep it the view. so, later on, I can easily find the file for edit.