Can someone help me out with this piece of code.
I have a template set up like this:
system/core/Loader.php :
public function template($view, $vars = array(), $return = FALSE)
{
$template = $this->view('includes/header', array(), $return);
$template = $this->view('includes/navigation', array(), $return);
$template .= $this->view($view, $vars, $return);
$template .= $this->view('includes/footer', array(), $return);
if ($return)
{
return $template;
}
}
It works perfect but when I try this to display a dynamic title, it doesn't want to display the title. I guess because the data is passed to content_home rather then to includes/header:
my controller:
public function home() {
$this->load->model("model_get");
$data["page_title"] = "Home Page";
$data["results"] = ($this->model_get->getData("home"));
$this->load->template("content_home",$data);
}
includes/header.php
<title><?php echo (isset($page_title)) ? $page_title : 'Default title text'; ?> </title>
Any ideas how to work it out ??
Thanks : )
In your views folder make a template file like this,
we'll call it template_web.php
<?php
// the preset view files for your template
$this->load->view('includes/header');
$this->load->view('includes/navigation');
// in this example we will have our content view files in a folder called 'web'
$templatefolder = 'web/';
// this is where you can pass in 'content' view files that are unique for the page
// this example has 3 placeholders you can put as many as you need
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
// preset template footer file
$this->load->view('includes/footer');
ok now in your controller methods
// data you want to pass to your header
$data['page_title'] = 'Awesome Home Page';
// other data
$data['bigbagofdata'] = $this->bigBagOf->awesomeData ;
// the name of the view files
$data['content01'] = 'homepage';
$data['content02'] = 'homepage_banner';
// your template
$this->load->view( 'template_web', $data );
data can be passed to any of the view files. and its very easy to set up different templates this way.
Try
$template = $this->view('includes/header', $view, $return);
instead of
$template = $this->view('includes/header', array(), $return);
I've implemented dynamic titles in an even simpler way:
Controller:
$data['header_data'] = array('title'=>'Foo bar');
$data['page'] = "login";
$this->load->view('template', $data);
View - template.php
$this->load->view('includes/header', $header_data);
$this->load->view('pages/'.$page);
$this->load->view('footer');
We have two folders in views - includes and pages. includes folder: header.php, footer.php (etc..) pages folder: login.php, profile.php (etc..)
views/includes/header.php
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
...
This way, you can use the same template to load all your pages. You may send data other than the title too!
If you want to access your $data["page_title"]
variable in your header view, then you should pass it into your header view:
change this line:
$template = $this->view('includes/header', array(), $return);
to
$template = $this->view('includes/header', $vars, $return);
I think MrMarchello finds oout the real issue. the issue is not passing the $data variable to header.
$data['title'] = "Edit User";
$this->load->view('template/header', $data);
$this->load->view('user/edit_member', $data);
$this->load->view('template/footer');
Hope this help you a little bit.
In your config file add this parameter:
$config['pageTitle'] = 'Book Store';
In template page :
<title><?=$this->config->config["pageTitle"]?></title>
In any page you like change this config in action:
$this->config->config["pageTitle"] = $detail[0]['Title'];
Every time you can change the title easily.