Basic Page Function - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Basic Page Function (/showthread.php?tid=16) |
Basic Page Function - Tux - 10-22-2014 Load "header", "footer" and "content" all at same time by using simple "page()" function. Site.php Model: Code: <?php defined('BASEPATH') OR exit('Access Denied'); Make sure you autoload this "Site.php" model in your config. You need to edit "application/config/autoload.php" file and add "site" to model autoload array. Usage in Controller: Code: <?php defined('BASEPATH') OR exit('Access Denied'); header.php view: Code: <!DOCTYPE html> welcome.php view: Code: <div id="content"> footer.php view: Code: <div id="footer"> RE: Basic Page Function - Rufnex - 10-22-2014 You can also load it also directly inside the view: some-page-template.php PHP Code: <?php $this->load->view('header'); ?> And here the call in the controller: PHP Code: $this->load->view('some-page-template', $this->data); Nothing new i think ;o) RE: Basic Page Function - John_Betong - 10-22-2014 Returning views as data From the manual: There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned: $string = $this->load->view('myfile', $data, true); echo $string; RE: Basic Page Function - John_Betong - 10-22-2014 Duplicated post RE: Basic Page Function - Rufnex - 10-22-2014 Yeah usefull for e.g. as email template RE: Basic Page Function - navotjer - 10-24-2014 I usually use MY_Controller and load the view like so (example only)(if anybody has some thoughts about my way, please, make me smarter ): Code: class MY_Controller extends CI_Controller Then in Some_controller: Code: <?php Thank you for the #EOF tip. *Edit -> BB code tags... RE: Basic Page Function - jlp - 10-25-2014 (10-22-2014, 12:48 AM)Tux Wrote: Load "header", "footer" and "content" all at same time by using simple "page()" function. Speaking as an educator, I see MVC as a design pattern to separate state from presentation from control, and I encourage my students not to mix things up. For instance, if my students submitted work where one of their models loaded any view stuff, they would be penalized marks. Similarly, if they submitted work where a view component accessed a database, they would be penalized marks too. To me, the proper place for the "page" method you show is in the controller. If this were something used frequently, I would go so far as to put it in a base controller (core/MY_Controller). Just trying to encourage good practices! RE: Basic Page Function - kilishan - 10-25-2014 I will create a simple `render` method in MY_Controller that allows for a very simple type of template system. It will auto-determine the the view to be used based on controller/method naming conventions. A simplified version looks a little like this: Code: protected function render($data=array(), $layout='') My current one is actually a bit more complex than that, and automatically formats and inserts status messages, gets stylesheet and script links to the view, etc. Then, I have different layout files in the views/theme folder that contain the overall layout, or template, to use. The content of the primary view for that method is made available to the layout in the $view_content variable. A simple layout might be like this: Code: $this->load->view('theme/parts/header'); You can create as many layouts as you need - for example, two-column layouts, or blog specific layouts, etc. But then using them in your controller is a single method call: Code: public function index() RE: Basic Page Function - puppy - 11-04-2014 (10-25-2014, 06:35 AM)jlp Wrote:(10-22-2014, 12:48 AM)Tux Wrote: Load "header", "footer" and "content" all at same time by using simple "page()" function. I tend to agree with this. Why use the MVC method if it is not going to be followed? What it comes down to for me is clean code design. Your "Page" function is something I would put in my base controller. This is certainly an excellent idea (and the same that I use), but in my opinion it is simply in the wrong place. |