Welcome Guest, Not a member yet? Register   Sign In
CI3 - wiredesignz HMVC
#5

Personally, I use a Template library. By using a library, you can avoid the Modules::Run() calls until you really need them, and possibly use the same code (or most of the same code) in a non-HMVC project.

The library I use may serve as an example, but I don't think it will currently work without some supporting code from Bonfire.

The relevant features are the handling of "layouts" and views. Basically, a layout specifies where any number of views will be loaded into a page and defines a content region on the page. The content region is where the current view will be loaded. The current view can be set, but defaults to the view named for the current method (in a directory named for the current module/controller). The library then has a render() method which loads the current view into the content region of the current layout and starts output (so it is typically the last thing called by your controller methods).

All you have to do is load the Template library in any controller which needs it (or in a base controller), and use the Template methods instead of $this->load->view(). In most cases, I'll end up with something like this:


PHP Code:
class Example extends MY_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();

        
// Set a layout other than the default 
        // to be used by this controller.
 
       Template::setLayout('no_sidebar');
 
   }

 
   public function index()
 
   {
        
// Load the view from 'example/index.php'
        // into the 'no_sidebar' layout.
 
       Template::render();
 
   }

    public function 
one()
    {
        
// Change the current view to 'example/two.php'
        // instead of 'example/one.php'.
        
Template::set_view('two');
        
        
// Load the current view from 'example/two.php', 
        // but use the layout 'sidebar.php'.
        
Template::render('sidebar');
    }


Of course, there are still cases where $this->load->view() comes in handy, such as loading a view to return to an AJAX request which shouldn't use the template.

I also probably wouldn't use static methods if I were building a library. There doesn't seem to be a significant benefit to it in a CI project.
Reply


Messages In This Thread
CI3 - wiredesignz HMVC - by ambush-reality - 06-03-2015, 09:18 AM
RE: CI3 - wiredesignz HMVC - by davicotico - 06-03-2015, 11:00 AM
RE: CI3 - wiredesignz HMVC - by ambush-reality - 06-03-2015, 12:20 PM
RE: CI3 - wiredesignz HMVC - by RogerMore - 06-04-2015, 12:36 AM
RE: CI3 - wiredesignz HMVC - by mwhitney - 06-04-2015, 07:13 AM
RE: CI3 - wiredesignz HMVC - by ambush-reality - 06-04-2015, 08:11 AM
RE: CI3 - wiredesignz HMVC - by mwhitney - 06-04-2015, 08:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB