Header and footer in CodeIgniter
Here's what I do:
<?php
/**
* /application/core/MY_Loader.php
*
*/
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
For CI 3.x:
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
if($return):
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
return $content;
else:
$this->view('templates/header', $vars);
$this->view($template_name, $vars);
$this->view('templates/footer', $vars);
endif;
}
}
Then, in your controller, this is all you have to do:
<?php
$this->load->template('body');
How to load page views like header, sidebar, footer in CodeIgniter?
in advance, you can through this way to build your view:
first create an Template in your view folder
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta content="MajidGolshadi" name="author">
<?php echo $html_head; ?>
<title></title>
</head>
<body>
<div id="content">
<?php echo $content; ?>
</div>
<div id="footer">
<?php echo $html_footer; ?>
</div>
</body>
</html>
second
create an library to load your view automaticaly
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template
{
private $data;
private $js_file;
private $css_file;
private $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->helper('url');
// default CSS and JS that they must be load in any pages
$this->addJS( base_url('assets/js/jquery-1.7.1.min.js') );
$this->addCSS( base_url('assets/css/semantic.min.css') );
}
public function show( $folder, $page, $data=null, $menu=true )
{
if ( ! file_exists('application/views/'.$folder.'/'.$page.'.php' ) )
{
show_404();
}
else
{
$this->data['page_var'] = $data;
$this->load_JS_and_css();
$this->init_menu();
if ($menu)
$this->data['content'] = $this->CI->load->view('template/menu.php', $this->data, true);
else
$this->data['content'] = '';
$this->data['content'] .= $this->CI->load->view($folder.'/'.$page.'.php', $this->data, true);
$this->CI->load->view('template.php', $this->data);
}
}
public function addJS( $name )
{
$js = new stdClass();
$js->file = $name;
$this->js_file[] = $js;
}
public function addCSS( $name )
{
$css = new stdClass();
$css->file = $name;
$this->css_file[] = $css;
}
private function load_JS_and_css()
{
$this->data['html_head'] = '';
if ( $this->css_file )
{
foreach( $this->css_file as $css )
{
$this->data['html_head'] .= "<link rel='stylesheet' type='text/css' href=".$css->file.">". "\n";
}
}
if ( $this->js_file )
{
foreach( $this->js_file as $js )
{
$this->data['html_head'] .= "<script type='text/javascript' src=".$js->file."></script>". "\n";
}
}
}
private function init_menu()
{
// your code to init menus
// it's a sample code you can init some other part of your page
}
}
third
load your library in every controller constructor and then use this lib to load you view automatically
to load additional js in your view you can use addJS
method in this way:
$this->template->addJS("your_js_address");
for Css:
$this->template->addCSS("your_css_address");
and to show your page content call your view file with show
method
$this->template->show("your_view_folder", "view_file_name", $data);
i hope this codes help you
In codeigniter how to get tcpdf custom header and footer
You need extend TCPDF in order to use custom header and footer
class PDF extends TCPDF {
//Page header
public function Header() {
// Logo
$image_file = K_PATH_IMAGES.'logo_example.jpg';
$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Even I did like this and got my result.
reference: https://tcpdf.org/examples/example_003/
Related Topics
Sorting a Multidimensional Array in PHP
Converting an Integer to a String in PHP
PHP Image Upload Security Check List
Get the Current Script File Name
Get Country of Ip Address With PHP
Find Highest Value in Multidimensional Array
Count How Many Files in Directory PHP
What Is the Meaning of Three Dots (...) in PHP
Are There Good Tutorials on How to Use Pdo
The Character Encoding of the HTML Document Was Not Declared
Laravel Orderby on a Relationship
What Is Faster: In_Array or Isset
Pdo - Call to a Member Function Prepare() on a Non-object
Generating (Pseudo)Random Alpha-numeric Strings