skip to Main Content

I am trying to set title, meta description and keywords dynamically using the language feature like below:

$lang['/contact_title'] = "Contact Us";
$lang['/contact_desc'] = "Description for contact us page";
$lang['/contact_keywords'] = "key, words";

and in header file I am using it like below:

<title><?echo $this->lang->line($_SERVER['PATH_INFO']."_title");?></title>
<meta name="description" content="<?echo $this->lang->line($_SERVER['PATH_INFO']."_desc");?>"> 

So far this is working great, but how I could make it work for dynamic titles? Regex was the first though that came in my mind, but unfortunately it doesn’t work with language classes like it works for routing as I have already tried it.

The reason i am using language class is because, its huge list, there are many controllers and we are going to change this seo keywords, title again and again. So to avoid that hassle i want all seo details in one place. So in future if we want to make changes, instead of visiting all the controllers i can make changes in one file only.

Can anyone suggest me an idea/solution of how I could make it work with dynamic titles?

2

Answers


  1. Try This

    On controller method add this:

    $data['contact_title'] = "Contact Us";
    $data['contact_desc'] = "Description for contact us page";
    $data['contact_keywords'] = "key, words";
    
    $this->load->view('header',$data);
    

    And finally call on header.php:

    <title><?php echo $contact_title; ?></title>
    <meta name="description" content="<?php echo $contact_desc; ?>">
    <meta name="keywords" content="<?php echo $contact_keywords; ?>">
    
    Login or Signup to reply.
  2. You should not use any of regex there. All data should be passed by controller, thats how MVC works. So, depending your situation, you should create variables in the controller and pass it to view. You can make it like this:

    in your language file (inside english directory for example):

    $lang['page_title'] = 'Home page';
    

    and after you switch languages (by default it was defined in your config file) you can access it via:

    lang('page_title');
    

    so the code in your controller should look like this:

    $data = array(
       'page_title' => lang('page_title')
    );
    
    $this->load->view('view_name', $data);
    
    // for usage in the view
    <head>
       <title><?= $page_title ?></title>
    </head>
    

    So using codeigniter $this->session->userdata('lang'); it will return current selected lang. You can change it:

    $this->session->set_userdata('lang','germany');
    

    and then system will read files from your application/language/germany/ folder. Do not forget to load language first – $this->lang->load('lang_file', $this->session->userdata('lang'))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search