Page Title PHP Script

Thanks for starting the forum guys! Exciting stuff.

Here’s a question to kick things off. When I create templates for mcms_page.php, mcms_sermonpage.php, etc., I usually drop in a different getContent() for the title on each template.

I’m thinking about writing a function using a few if statements that would check the url to see if it’s a Sermon Detail page, Article Detail page, etc., and it’s not a special page then default to getContent(“page”,“show:title“).

Here’s a very rough version.
function writePageTitle() { $sermonSlug = getContent("sermon","display:detail","find:".$_GET['sermonslug'],"show:__title__", "noecho"); $articleSlug = getContent("article","display:detail","find:".$_GET['sermonslug'],"show:__title__", "noecho"); $pageSlug = getContent("page","find:".$_GET['nav'],"show:__title__", "noecho"); if($sermonSlug) { echo $sermonSlug; } else if($articleSlug) { echo $articleSlug; } else { echo $pageSlug; } }

Any drawbacks to this approach?

The benefits as I understand it would be I could just drop in writePageTitle(); in the head of the file and go on my merry way.

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:

  1. 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.
  1. 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