How to create a home page (layout) with a basic design

I am new to CodeIgniter. I want to create a main page or layout with a basic style that will contain a menu, footer, etc. I don’t want to write duplicate content on all pages and automatically download it for all pages. For example, I can create a master page in asp.net or a layout in asp.net mvc. I am sure I can do this in CodeIgniter.

+7
codeigniter
source share
4 answers

suggests that you have an html page

<html> <head> <title> Hello World </title> </head> <body> <div id="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </div> <div id="main-content"> <!-- this is the dynamic part --> </div> <div id="footer"> Copy Right 2013 Hello World </div> </body> </html> 

you could break it into 1- heading 2- menu 3- main content 4- footer

you basically put

 <html> <head> <title> Hello World </title> </head> <body> 

in one view called "view_header" then you put

  <div id="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </div> <div id="main-content"> 

in a view called "view_menu" and then you put

  </div> <div id="footer"> Copy Right 2013 Hello World </div> </body> </html> 

in a view called "view_footer" then in your controller

 $this->load->view('view_header'); $this->load->view('view_menu'); $this->load->view('YOUR_VIEW'); $this->load->view('view_footer'); 

Another solution I see is better: create a view called view_template_1.php

 <html> <head> <title> Hello World </title> </head> <body> <div id="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </div> <div id="main-content"> <?php $this->load->view($content); ?> </div> <div id="footer"> Copy Right 2013 Hello World </div> </body> </html> 

in the controller you can say that you want to call a view called About

 $data = array('content'=>'about'); $this->load->view('view_template',$data); 
+14
source share

Dynamic layout

You must create a new controller with the variable public $ template. Then your advanced controller inherits the variable $ template from the main controller.

MY_Controller

 class MY_Controller extends CI_Controller { public $template=null; public function __construct() { if(is_null($this->template)){ $this->template = 'layouts/default'; } } } 

Admin_controller

 class Admin_Controller extends MY_Controller { public function __construct() { //Still inherits from MY_Controller //this time, any controller extending Admin_Controller //will point to 'views/layouts/admin' if(is_null($this->template)){ $this->template = 'layouts/admin'; } } } 

-

 class User extends MY_Controller { public function index() { //$this->template is inherited //from MY_Controller //which point to 'views/layouts/default' //We can also load a view as data //ie no master layout, INTO our master layout //note we don't pass $this->template //and set the third param as true $dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array( 'data' => 'some_data' ), true); return $this->load->view($this->template, array( 'partial' => 'users/index' //partial view, 'dynamic_sidebar' => $dynamic_sidebar )); } } 

Views / Layouts / Default

 <body> //load the main view //in our example we have also loaded //a dynamic sidebar with this view <?php $this->load->view($partial); ?> <?php $this->load->view($dynamic_sidebar); ?> //load a static view //views/sidebar/static <?php $this->load->view('sidebar/static'); ?> </body> 
+3
source share

Following the idea of ​​the main page in Laravel , we can do this:

Controller code

 $this->load->view('show'); 

View "show.php"

Set the values ​​for the main page, and then pass the variables to the main page. Save the homepage codes inside ob_start () and ob_get_clean () .

 <?php $page_title = "My first page"; ?> <?php ob_start(); ?> Your header stylesheet links and scripts <?php $page_header = ob_get_clean(); ?> <?php ob_start(); ?> <div> <h1>This is a Header</h1> <?php $this->load->view('Partials/list'); ?> </div> <?php $page_content = ob_get_clean(); ?> <?php ob_start(); ?> Your footer html or scripts <?php $page_footer = ob_get_clean(); ?> <?php $this->load->view('Layout/app',array( 'page_title' => $page_title, 'page_header' => $page_header, 'page_content' => $page_content, 'page_footer' => $page_footer )); ?> 

Partial view of "Partials / list.php"

If you do not want your code to be full. You can create some partial views to keep things simple.

 <ul> <li>LIst item 1</li> <li>LIst item 2</li> <li>LIst item 3</li> </ul> 

Master Page / Layout .php

 <html> <head> <title><?= v($page_title) ?></title> <?= v($page_header) ?> </head> <body> <?= v($page_content) ?> <?= v($page_footer) ?> </body> </html> <?php function v(&$var) { return isset($var) ? $var : ''; } 

So this will generate the code:

 <html> <head> <title>My first page</title> Your header stylesheet links and scripts </head> <body> <div> <h1>This is a Header</h1> <ul> <li>LIst item 1</li> <li>LIst item 2</li> <li>LIst item 3</li> </ul> </div> Your footer html or scripts </body> </html> 
+2
source share

What we are probably doing is separate view files for the header, menu, footer, etc., which is common to all pages. And include them in each view. as

 $this->view('header'); $this->view('menu'); //Some specific content $this->view('footer'); 

If you need the same functionality without copying the above to all views, you need to create a function in your controller as follows:

 private function myviewfunction($current_view) { $this->load->view('header'); $this->load->view('menu'); $this->load->view($current_view); $this->load->view('footer'); return NULL; } 

and call this function on all your pages (methods)

 $this->myviewfunction('about'); //About is the specific view for the method 
0
source share

All Articles