Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Dynamic site title per page

I am hard coding my site title at the moment but want to make it dynamic as every page requires header.php so every page has the same title tag. I currently define it in the config file like this:

define('SITENAME', 'My Website');

Open in new window


But for example, on the contact page I might want it to just say 'My site | Contact us'. Obviously this is a contrived example but I am trying to keep this simple.

Not sure if I should store page titles and meta description etc. in the database or just use a switch statement in the config file to say if the page is this then the title is this, or if the page is that then the title is that etc.

Any suggestions welcome!
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

Thanks Chris, love the MVC idea (I am using MVC) where I would pass it from the controller to the view but I don't think that will work because the title tag lives in my header.php file which is included/required into each page. So, that would mean I would have to change my site so that every page had the title tag and it wasn't in the header.php file anymore.

My header.php starts with <!DOCTYPE html> and ends after the navigation code.

That is really the nicest idea but not sure I want to have to restructure all my code. hmmm.
Hey,

Yes - you would pass the variables down from the controller. Then when you load the views, those variables would be accessible. It shouldn't matter that your header is included - the variables should still be available to the included file. I don't know your specific setup, how you're including the header file, or how you'r calling your views, so I can't be specific.

If you load all your views separately from your controller, then just pass in the variables to each view. If you load up the main view only, and include the header on that page, then you'd only need to pass the variables to the main view (and they should be accessible to any includes).

Maybe if you could explain how you load up the views / includes etc, I might be able to help out a little more.
Sorry Chris, I should have been more specific. I however tried what you said and it works perfectly. I just have to be sure to pass every single page a title in the controller otherwise it displays a nasty error in the browser tab instead of the proper title. Works perfectly!
Or I guess I could just have an isset check to default to a title if one isn't passed in.
Thanks!
No worries :)