Page Title PHP Script
Hi mjatharvest,
I definitely see the use in creating a central function for determining the page title. However, I think there are some major drawbacks to consider:
- You’re always doing two getContent() calls that aren’t necessary. This will slow down the page as it waits to receive an HTTP response from our API.
- As you add more and more content types to writePageTitle() — such as blogs, discussions, etc. — you’ll end up slowing down the page more and more.
So, while it certainly makes it easy to get the page title, I think it adds some unnecessary overhead to the page.
Hope that helps!
Justin “Jark” Stayton
Monk Development
mjatharvest,
I haven’t tried using one central function for determining the page title but you might be able to look at the $_SERVER[‘SCRIPT_NAME’] variable to determine which script is being called and using a switch statement based on that. So:
<?php
$template = $_SERVER['SCRIPT_NAME'];
switch($template):
case 'ekk_sermonpage.php': $pageTitle = getContent(...);
break;
case 'ekk_articlepage.php': $pageTitle = getContent(...); break;
case 'index.php': $pageTitle = getContent(...); break;
// for regular mcms_page.php default: $pageTitle = ...; break;
endswitch;
?>
This way, you could just add a new case to the switch statement every time you add a new template. I haven’t tested it this at all but it might work and would eliminate the use of system resources that justin mentions in his post.
Ben
Edited by benotero on 8/4/08
Thanks guys. Some good thoughts there.
benotero
That looks like it could be a good solution. I’d have to brush up on the syntax of switch statements before I implemented it.jstayton
What about passing a variable to the writePageTitle() function? Maybe something like this:@// in an included file
$artictleTitle = getContent(“article”...);
$pageTitle = getContent(“page”...)$writePageTitle($title) {
echo $title;
};// in the template
<title>$writePageTitle($artictleTitle);</title>@
You’re still doing the same amount of unnecessary API calls as before. I think Ben’s solution is a good one, basing the output on the template file name rather than which API call returns a title. Then you know exactly which API call to make.
Justin “Jark” Stayton
Monk Development
Thanks, this has been very helpful.
Here’s a work in progress script that I’m planning on implementing. Hope it benefits someone else as well.
@function writePageTitle($MCMS_SITENAME) {
$template = $_SERVER[‘SCRIPT_NAME’];
switch($template) { // Content Pages case “/mcms_page_level1.php” : $pageTitle = getContent(“page”, “find:”.$_GET[‘nav’], “show:title“, “noecho”); break; case “/mcms_page_level2.php” : $pageTitle = getContent(“page”, “find:”.$_GET[‘nav’], “show:title“, “noecho”); break; case “/mcms_page.php” : $pageTitle = getContent(“page”, “find:”.$_GET[‘nav’], “show:title“, “noecho”); break;
// Detail Pages case “/ekk_articlepage.php” : $pageTitle = getContent(“article”,“display:detail”,“find:”.$_GET[‘sermonslug’],“show:title“,“noecho”); break; case “/ekk_events.php” : $pageTitle = getContent(“event”,“display:list”,“enablepast:yes”,“year:”.$_GET[‘year’],“month:”.$_GET[‘month’],“day:”.$_GET[‘day’],“beforeshow:date format=‘F j, Y’“,“noecho”); break; case “/ekk_eventpage.php” : $pageTitle = getContent(“event”,“display:detail”,“find:”.$_GET[‘slug’],“show:title“,“noecho”); break;
// Default default : $pageTitle = getContent(“page”, “find:”.$_GET[‘nav’], “show:title“, “noecho”); endswitch; } echo “<title>{$pageTitle} :: {$MCMS_SITENAME}</title>”;
}@
Edited by mjatharvest on 8/6/08
Justin, could I also use if/else conditions instead of switch/case as long as I’m testing against the $SERVER[‘SCRIPT_NAME’] variable.
Overall I don’t have to worry about API calls if they are within conditional blocks of code like “if and” “switch”, is that right?
page 1 of 1
discussions 1 to 7 of 7
on 8/4/08 mjatharvest wrote: