CI3 - wiredesignz HMVC |
Hello,
I've just setup a local copy of CI3 & installed wiredesignz HMVC and following the installation instructions I have the welcome message working as it should, I'm just getting started and would like to be able to include a single header.php file & footer.php to use across the application, but unsure where to actually put them within the application. I currently have the following: /application/modules/welcome/controllers/Welcome.php /application/modules/welcome/views/welcome_message.php /application/modules/templates/views/header.php /application/modules/templates/views/footer.php If I put Code: <?php require_once 'header.php'; ?> and I can keep them in one place Regards
You need to create a controller Templates with the method header() and footer()
With hmvc basically you call the method header and footer in any views: <body> echo Modules::run('template/header'); <p>Something...</p> echo Modules::run('template/footer'); </body> I have an article about this (in spanish) http://davicotico.com/configurar-codeigniter-y-hmvc/
Hi Davicotico,
Thanks for the reply, I'll look at this more in detail, although I don't speak Spanish I can kind of figure it out what you mean. Regards
Hello ambush-reality, welcome to the forum...
I think that Davicotico misunderstood your problem. First of all, when working with views you never use required or include to get the view in your project. The normal way is to use $this->load->view('view_name');. Because you are using HMCV you can load a view almost the same way. HMVC sometime can sometimes find it without help when you use only the name of the view, but it's easier just to point load in the right direction by addressing the module where the views is located. In your case: PHP Code: $this->load->view('module_name/view_name'); If you have pass data to the view, it works the normal way like so: PHP Code: $data = array(); The modules::run() solution Davicotico is referring to is a way to echo the output of a method from a certain module. This is not necessarily needed. Hope this helps! -Roger
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 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.
Thanks everybody,
Some useful comments here to take on board, some a little complex for my tiny little brain to cope with, I'm new to CodeIgniter and relatively new to PHP, so working through the user guide although in past projects I always built in a module way so to me it makes sense, so off to build a small application to see how it all fits together... One note I'm struggling with, which I haven't found a solution to either using htaccess or PHP or in the user guide, i've connected to the mysql database and figured how to create a menu based on how the news tutorial using pages/controllers/Pages.php pages/models/Pages_model.php pages/views/index.php & pages/views/views.php so querying the database pages table I can view the individual content on view.php, but how do I get the home page text to pull through from the database on the index.php without using static content?? Regards
http://www.codeigniter.com/user_guide/ge...o-the-view
Load the data in your controller, add whatever you want from the loaded data to an array of key => value pairs, and pass the array to your view. The keys in the array will be converted to variable names which you can use in your view to access that value in your view. PHP Code: // Controller A template library can include methods to add this data if it calls $this->load->view() for you. For example, Bonfire's Template library uses Code: Template::set('variableName', 'value'); |
Welcome Guest, Not a member yet? Register Sign In |