I'm new to CodeIgniter and I'm very confuse on how to load my header.php to all the other pages I made. I want my header to be included on every page I made without manually putting them so that every time I make changes to the header I won't edit the entire page. Instead I'll just make changes in the header.php. Thank You.
create a common page call layout.php
inside that
<?php $this->load->view('includes/header'); ?> //site header
<?php $this->load->view($main_content); ?> //comes from controller.
<?php $this->load->view('includes/footer'); ?> //site footer
and in controller
function privacy_policy()
{
$data['main_content'] = 'pages/privacy_policy';
$this->load->view('layout', $data);
}
and main_content should direct to page
so its
reduce your time
reduce your code
easy to understand
etc....
You can create a folder for example -Layout under view folder and add your common pages like header,footer in it.
And in your controller on calling you can do so-
function index() {
$this->data['survey'] = $this->survey_model->get_survey();
$this->data['main_content'] = $this->controller . '/show';
$this->load->view('layouts/main_content', $this->data);
}
In this case show is my view page to be displayed,main_content is a page in my layout where header and footer is mentioned.
There are many ways but the one I prefer the most is as below :
in your Controller File
SomeController.php
function someFunction() {
$this->data['main_page'] = 'someview';
$this->load->view('layouts/main_template', $this->data);
}
In your main_template.php view
<?php
$this->load->view('layouts/header'); // your common header file
// your dynamic page file you can choose which page to load by changing the value of $main_page in controller.
$this->load->view($main_page);
$this->load->view('layouts/footer'); // your common footer file
?>
Related
I am new to codeigniter ,
I want to redirect Login page request to this route
$route['login'] = 'TravelApi/login/';
So now http://localhost.com/codeigniter/login request should route through controller/TravelApi.php 's TravelApi class 's login() function.
Controller
public function login(){
$contents['login_url'] = $this->googleplus->loginURL();
$this->load->view('frontend/login',$contents);
}
My question is :
When request routes through above controller and then goes to frontend/login.php -- login.php file gets loaded but without header and footer.
But when I remove this route from config/routes.php
$route['login'] = 'TravelApi/login/';
then request does not route through controller and directly goes to frontend/login.php . and here it loads login.php file with header and footer.
But my need is to route from controller. and load view file with header footer.
So why it does not load header footer when routes through my controller's function ?
EDIT:
I found a function in default controller welcome.php
public function pages($alias=NULL)
{
$page='frontend/'.$alias;
$this->load->view('frontend/common/head'); // For Head Scripts
$this->load->view('frontend/common/header', $this->common_menu('TopMenu')); // For Header Content
$this->load->view('frontend/common/menus', $this->common_menu('MainMenu')); // For Menus
$this->load->view($page);
$this->load->view('frontend/common/footer'); // For Footer Content
$this->load->view('frontend/common/foot'); // For Footer Scripts
}
But still not clear that why it does not load header footer when routes through my controller's function ?
You kind of answer your own question in your edit. The header and footer are located in seperate view files, and you need to load those too. So something like this should work:
public function login(){
$contents['login_url'] = $this->googleplus->loginURL();
$this->load->view('frontend/common/head'); // For Head Scripts
$this->load->view('frontend/common/header', $this->common_menu('TopMenu')); // For Header Content
$this->load->view('frontend/common/menus', $this->common_menu('MainMenu'));
$this->load->view('frontend/login',$contents);
// For Menus
$page='frontend/'.$alias;
$this->load->view($page);
$this->load->view('frontend/common/footer'); // For Footer Content
$this->load->view('frontend/common/foot'); // For Footer Scripts
}
Note: The line $this->load->view($page); looks like it's probably the main content for the other page and here the main content should be $this->load->view('frontend/login',$contents);, if that's the case remove the $page view load.
I have a controller which has following function
public function listartistes () {
$this->set_language();
$this->data['title'] = "Create Your Own Show Today";
$this->data['meta_title'] = "Create Your Own Show Today";
$this->data['meta_desc'] = "Create Your Own Show Today";
$this->data['content'] = 'artiste/list';
$this->data['artistes'] = $this->artistes_m->getAllArtistes();
$this->load->vars($this->data);
$this->load->view('template');
}
As you can see the controller is loading the template view and inside template view we load the 'content' file like this this
<div class="container-full">
<?=$this->load->view($content);?>
</div>
So far so good . but when I try to put any PHP code in list which is inside
/artiste/list its not showing on my browser only blank screen
The partial pattern in CodeIgniter allows consecutive views to be loaded, not embedded ones. So this means that you might need a partial for header, content and footer for example instead of an outer template and inner [content] template.
So while you may have to adjust what markup and data is in which view, you will probably be best off putting
$this->load->view($content); on the line after $this->load->view('template'); in the controller. I don't think that CodeIgniter is capable of loading views from within views.
Can someone help me to explain in which way is the best to organize views in codeigniter and make some template where header and footer will be same but content to change for every page? I use parse to send data from controller to view but don't know how to organize view in described way.
Also how to avoid php logic in view (if)?
There are two ways if your are not using a template library:
1. Request your partial views in your content view:
<?php $this->load->view('header') ?>
My content
<?php $this->load->view('footer') ?>
2. Create a layout view, pass the content as variable:
<html>
<body>
<?php echo $content ?>
</body>
In this case, in your controller:
// The true parameter means it will load the view, but just pass back as a string
$content = $this->load->view('mycontent', array(), TRUE);
// Display the layout with your content
$this->load->view('layout', compact('content'));
As asking about the logic, don't get too strict with it. if, for, foreach are okay in a template. Forexample if a user is logged in, show a menu.
This might help you regarding about the templating.
Controller:
public function index() {
$data = array(
'blog_title' => 'Home',
'blog_content' => 'Content goes here..',
'footer_data' => 'footer data here..'
);
$this->load->view(templates/header, $data);
$this->load->view(templates/content, $data);
$this->load->view(templates/footer, $data);
}
Note: imagine that templates is a directory inside your views folder. Inside your templates must be the header, content and footer.php files.
I wish to display a variable that is stored in a session at the top of each page throughout my website. At the minute, on every single page, in the controller index() I have;
$data['credits'] = $this->session->userdata('credits');
I havve created a seperate view for the navigation bar (where the variable will be displayed). I have called it vNav.php. In vNav.php I then do echo $credits.
For every new view, I have to include the vNav.php, but that also means in the other view's controllers, I have to set the $data['credits'] variable in the index() function.
Is there a way in CI to do this automatically for me? So I don't have to have the same line of code in all my controllers?
Thanks
Okay here's a better design for your views.
First, create a folder called include/, in this folder create a header.php, footer.php, template.php.
header.php
<html>
<head>JS - CSS - META</head>
<body>
<div>COMMON MESSAGE</div>
footer.php
<footer>FOOTER GOES HERE</footer>
</body>
</html>
template.php
$this->load->view('include/header.php');
$this->load->view($main_page);
$this->load->view('include/footer.php');
any controller:
$data['main_page'] = "hello_world"; // view/hello_world.php
$this->load->view('include/template',$data);
So in that way, you can create a common section that will be displayed on all pages by adding it to the header or the footer. If you want, you can also create another file in the include folder and then include it in the template so that it will be loaded automatically every where by only modified one file
Let say I separate my page into 4 parts. One is banner, one is login, one is main, another is footer. Banner and Footer is static, it won't change. and login is based on different user right or login status, lastly the main is based on many thing to do it. So, I have 4 parts of element:
http://localhost:8888/my_app/index.php/static_page/banner
http://localhost:8888/my_app/index.php/static_page/footer
http://localhost:8888/my_app/index.php/user/login_fragment
http://localhost:8888/my_app/index.php/system/main_fragment
But the user only see one page, but it combine all the staff in...So, instead of using iframe to put them together, how can I let these page separate the logic, but can combine in one view? Thank you.
I want to share what I have done. Although not an original idea(do not remember from where I got), hopefully this will help you. I have an 'includes' [application/views/includes] folder, which contains the following files:
footer.php, header.php, navigation.php and template.php
Now the code in the template.php is as the following:
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
For usage, let me give you an example:
$data = array (
'page_title' => 'Users Listing',
'title' => 'Users Listing',
'main_content' => 'users/showlist',
);
$this->load->view('includes/template', $data));
The elements in the $data array, will be available in the view [application/views/users/showlist.php] as $page_title, $title etc. You can also send arrays or HTML content.
Just call load->view multiple times
in your controller method:
function index()
{
$this->load->view("banner",array(/** some parameters if you want **/));
$this->load->view("main",array(/** some parameters if you want **/));
$this->load->view("footer"); // this one doesn't need parameters maybe
}
See the views documentation page
Note: within a view file itself (e.g. inside banner.php) you can call load-view again to insert a nested view.
Note2: you can also return the result of load->view into a variable if you don't want to output it right away, by adding a true parameter as the 3rd parameter:
$login_box = $this->load->view("login",array(),true);
// $login_box contains the view html.
echo $login_box; // or pass it as a parameter to another view if you want
First of all
$this->load->view('filename');
Can be used as many times as you want in the same controller function, and they will appear in the right order.
What you did is separate in controllers parts that must be used in the same controller. I suppose you have these in files like
/application/controllers/static_page.php
/application/controllers/user.php
/application/controllers/system.php
And, without massive modification of CodeIgniter, you can't call a controller from another controller, which is what was stopping you. If you have to use the same processing code for many functions, you have to make either a helper, that you can use anywhere, or the more elegant solution, that is having common controller functions in /libraries/MY_Controller.php.
MY_Controller.php would be called always before a controller is called
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// you can do any kind of processing here
// in example if you have some view counter
// this place is always called
}
function a_generic_function($rawdata)
{
// do some processing here
return $data;
}
}
Then, you will be able to use this function in any of your controllers:
$this->a_generic_function($rawdata);
Now, the smartest way to deal with a header+footer is making a "layout" file that contains both the header and the footer.
Here's an example of layout.php view:
<!doctype html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<header>This page has been made by <?php echo $user_name; ?>/header>
<div id="main" role="main">
<?php echo $main_content_view; ?>
</div>
<footer>Write your footer here</footer>
</div>
</body>
</html>
You see there's $main_content_view in the middle.
With this you will do, in your controller:
// add the HTML for the login to the variable
// TRUE means we don't output it in the browser, but put it in a String
$this->viewdata["main_content_view"] = $this->load->view('login', $data, TRUE);
// want to add more data? just ADD it to the variable, like a String
$this->viewdata["main_content_view"] += $this->load->view('main', $data, TRUE);
// done? send it to the layout, and pass it the $this->viewdata
$this->load->view('layout', $this->viewdata);
What else can you do with this? You can use the $this->viewdata like:
$this->viewdata["title"] = 'Function title';
$this->viewdata["user_name"] = $user_name;
so you can edit your layout as much as you want like any other view.
Phil Sturgeon has written a popular template library for Codeigniter which will do what you want in a smarter way than what has been described in the other answers so far. You can read its documentation here: http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
The documentation might take a while to understand because it lacks examples.
You can download the template library here: https://github.com/philsturgeon/codeigniter-template