Crazy Horse
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:
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!
define('SITENAME', 'My Website');
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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!
ASKER
Or I guess I could just have an isset check to default to a title if one isn't passed in.
ASKER
Thanks!
No worries :)
ASKER
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.