PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] Setting page title from content file in CodeIgniter (MVC)

 March 05, 2022     codeigniter, php, templates     No comments   

Issue

My CodeIgniter website's controller is set up like this:

$this->load->view('head', $data); // Contains <head> stuff
$this->load->view('header', $data); // Nav and sidebars
$this->load->view($this->uri->segment(1), $data); // Main page content
$this->load->view('footer', $data); // Footer

My question is about where to store <title> tag's content. Ideally it would be with the page content (the 3rd loaded view) but then how do you get that into the <title> tag which has been loaded in the first view?

I've read that it's bad to have JavaScript set the title as it won't be compatible with search engine bots.

I've also read a suggestion which said to have a large view file with all the pag


Solution

I have thought of a way to solve my issue, and I got the idea from http://joshhighland.com/blog/2008/11/09/how-i-do-layouts-and-views-in-codeigniter/. If you look on that page you can see a line like this near the bottom of the first code extract:

$layout_data['content_body'] = $this->load->view('home/homePage', $body_data, true);

This loads the view as a string into that variable. I will then run a function on that string to extract what I want the title to be (I will store it in an HTML comment) and put it into $layout_data['pageTitle']. Then, by following the rest of the layout method on the link, I will be able to solve my issue.



Answered By - Chro
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing