Welcome Guest, Not a member yet? Register   Sign In
Why is page title not working?
#1

[eluser]Kraig[/eluser]
I looked around and found some threads that mentioned this, but for reason it's not working. Here is what I have

Public_Controller.php:
Code:
class Public_Controller extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    $this->data['pageTitle'] = 'Test Title'; // pages without a title should display this
    ....
    }

}

Home.php (my default controller...extends Public_Controller):
Code:
class Home extends Public_Controller {

public function __construct() {
        parent:: __construct();
}

function index()
{
  $this->data['pageTitle'] = 'Your page title';
  
  $data['main_content'] = 'home';

   // My header which contains the <title> is in the template
  $this->load->view('includes/template', $data);
}

}

The above works if I do this as it should, but I want pages to display a title if they don't have this set:
Code:
$data['pageTitle'] = 'Your page title';

Header area:
Code:
<title><?php echo $pageTitle; ?></title>

Any idea why this is not working?
#2

[eluser]Future Webs[/eluser]
Code:
$this->data['pageTitle'] = 'Your page title';

should be

Code:
$data['pageTitle'] = 'Your page title';

sorry, just fully read what you posted.

you could set a default title in your config and then make a function to check for a new title
#3

[eluser]CroNiX[/eluser]
Because you pass $data (and not $this->data) to your view, so that's what's available to it. In your view you'd have to access $this->data directly since you're not passing it.

Or if you want it to work the way I think you want it to, change everything to $this->data and pass $this->data to the view.

Then in the construct of your base controller, you can set the default title as you are, and then override it in your actual controller if a title exists.

Like in the constructor of the base controller:
Code:
$this->data['pageTitle'] = 'my default title';

Then in the actual controller:
Code:
if (some condition)
{
  //override the default
  $this->data['pageTitle'] = 'my custom title';
}
$this->load->view('your_view', $this->data);
#4

[eluser]CroNiX[/eluser]
or in your view, just change:
Code:
<title><?php echo $pageTitle; ?></title>
to
Code:
<?php //If the page title is set use it, else use the default set in $this->data
$title = (isset($pageTitle)) ? $pageTitle : $this->data['pageTitle']; ?>
<title><?php echo $title; ?></title>

although my original suggestion avoids needing this kind of logic in the view, especially if you have to do it for more than just pageTitle.




Theme © iAndrew 2016 - Forum software by © MyBB