1

I am working on a CI + HMVC installation. When I use the HMVC function call:

Modules::run("header");

It prints nothing to screen. However if I run:

echo Modules::run("header");

or:

$x = Modules::run("header");

Then it works.

This is the code that I am trying to make work:

// HOME MODULE
class Home extends MX_Controller{
    public function index(){
        Modules::run("header");
        $this->load->view('home_view');


        Modules::run("header");
       }
   }
// HEADER MODULE
class Header extends MX_Controller{
    public function index(){
        $this->load->view('header_view');
    }
}

// FOOTER MODULE
class Footer extends MX_Controller{
    public function index(){
        $this->load->view('footer_view');
    }
}

But when I run it I only see the "home_view" content. There is no header and no footer.

I cannot use the above approach to solve it, because $this->load->view() is buffered, which makes my "home_view" content appear at the bottom of my HTML, below my footer, and this mangles things.

Please help me find out why Modules:run() will not buffer. Thanks

Mehul Kuriya
  • 608
  • 5
  • 18
mils
  • 1,878
  • 2
  • 21
  • 42
  • Using like `echo Modules::run("header");` this is rule to render view partial as said in documentation. So why you dont want to use it? – Rejoanul Alam Oct 19 '16 at 06:25

1 Answers1

3

Yes, it does exactly as you describe, which is interesting and something I have never observed as I wouldn't implement what you have done in a real world script.

What you could do ( and this works because I have tried it ) is to create a template_view like so... And this is very cut down...

template_view.php

<?= isset( $header ) ? $header : ''; ?>

<?= isset( $content ) ? $content : ''; ?>

<?= isset( $footer ) ? $footer : ''; ?>

Then you would build your page sections and create the final output.

Home.php Controller

class Home extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        // Build the Page Sections
        $data['header']  = Modules::run("header");
        $data['footer'] = Modules::run("footer");
        $data['content'] = $this->load->view('home_view', '', true);
        // Display the final Page
        $this->load->view('template_view',$data);
    }
}  

So you could create your template to already include your header and footer , as they are static content in your example. Then just create regions that you fill in dynamically.

This would then lead you to using a Template Module to handle all of this for you. Which wasn't your question but hope it gives you some food for thought.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28