Welcome Guest, Not a member yet? Register   Sign In
Set different page title for every other page
#5

You could handle this any number of ways. What I usually do is set a default page title in my controller's constructor, then change the title as needed in the controller's methods (in most cases, the methods just add something to the title set by the constructor, since all of the pages served by a controller are usually related).

Then I use a function to load my data into my view and call it from all of the controllers, so I can get some defaults if I forget to set something in a given method or don't want to repeat certain things in each method.

For example:

PHP Code:
class News extends MY_Controller
{
 
  private $data = array();
 
  private $pageTitle;
 
  public function __construct()
 
  {
 
      parent::__construct();
 
      $this->pageTitle lang('news_default_page_title');
 
  }
 
  public function index()
 
  {
 
      // ... do something ...
 
      $data['page_title'] = "List : {$this->pageTitle}";
 
      $this->render($data'index');
 
  }
 
  public function details($id 0)
 
  {
 
      // ... do something ...
 
      $this->render(); // Load $this->data into the 'News/details' view...
 
  }
 
  protected function render($data = array(), $view '')
 
  {
 
      if (empty($view)) {
 
          // Load the view named for the current controller/method.
 
          $view "{$this->router->class}/" . (empty($this->router->method) ? 'index' $this->router->method);
 
      }
 
      $data array_merge($this->data, (array) $data);
 
      if (empty($data['page_title'])) {
 
          // Set the page title.
 
          $data['page_title'] = $this->pageTitle;
 
      }
 
      // ... other stuff ...
 
      $this->load->view($view$data);
 
  }

Reply


Messages In This Thread
RE: Set different page title for every other page - by mwhitney - 02-02-2015, 02:35 PM



Theme © iAndrew 2016 - Forum software by © MyBB