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

Can somone help me how to set different page title for each page? i am new with codeigniter and i make my website with the tutorial from codeigniter, but i am confused how to make title page for each page, maybe from controller or so?

thx for your help
Wiyono
Reply
#2

ooh, no aswer? please help me ;-)
Reply
#3

Yeah, you just pass data from your controller to your view.
Code:
//controller
$data['page_title'] = 'your page title'; //will be available as $page_title in view
$this->load->view('some_view', $data);

//view (some_view.php)
<title><?php echo $page_title; ?></title>
Reply
#4

Hey CroNiX,
thx for your answer, that's mean i have to make each controller for each pages?
ohye, can i make only one controller for all page or so?

thx again for your answer?

Regard
Wiyono
Reply
#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
#6

Hey mwhitney,
thx for your help
i have solution like this

PHP Code:
class Pages extends CI_Controller {
    function 
index() {
        
$data['title'] = "Startseite";
        
$this->load->view('templates/header'$data);
        
$this->load->view('pages/startseite'$data);
        
$this->load->view('templates/footer'$data);
        }
    function 
about_us() {
        
$data['title'] = "About us";
        
$this->load->view('templates/header'$data);
        
$this->load->view('pages/about-us'$data);
        
$this->load->view('templates/footer'$data);
        }

some change in routers. and work good, but the url address is "about_us" can you help me how to make the url address "about-us", when i put
PHP Code:
function about-us() { 
i got php error, can you help me please how to make the url address "about-us", with "-" and not "_" (underscore)

Thx again
Regard
Wiyono
Reply
#7

(This post was last modified: 02-02-2015, 03:56 PM by CroNiX.)

You can't have a dash in a function/class/method name. To PHP, that's subtraction.

Basically you create a route, using the dash, that routes to whatever controller/method, but the method name must be legal PHP.
Code:
$route['about-us'] = 'controller/about_us'; //Sends 'about-us' request to the controller/about_us controller/method with the underscore

Controller method:
Code:
function about_us()
{
  //your about-us page
}

Code:
<a href="/about-us">About Us</a> //clicking will actually go to "about_us" behind the scenes due to our route
Reply
#8

Hey CroNiX,
thx for your answer, unfortunately i got error 404 page if i make the router like that, then i call about-us page. Can you help? what i did wrong?

Thx
Regard
Wiyono
Reply
#9

post your route and the controller (not just the method)
Reply
#10

Hey CroNiX....

yeeeeeeeeezzzz...
thank you so much...
i forget to change the controller with the name of controller...

thank you again.

Regard
wiyono
Reply




Theme © iAndrew 2016 - Forum software by © MyBB