Jump to content

Recommended Posts

Posted

I have a PHP template set up that calls another file for the body of the page. That's all well and good, but I also want to change the title of the page, which of course is not in the body. It would be super cool and easy if I could just put an <? echo $title; ?> in the template title and then define the title in the body file. Everything is in one place for one page. Of course, it doesn't work the way I want because I'm defining the variable after it's been called.

 

Is there any way to do what I want to do with a little bit of trickery? Or fail that, by whatever means?

 

Thanks.

Posted

I do it the other way

My page defines the title and then includes the header

which can then display the title variable.

 

my template for a page

 

><?php
// add header and additional title
$page_title="home";
include $_SERVER['DOCUMENT_ROOT']."/header.php";
// begin main content
?>

<p>Content</p>

<?php
// end main content 
// add footer file 
include $_SERVER['DOCUMENT_ROOT']."/footer.php";
?>

 

then in the header.php

><HEAD>
<title><?php print("$site_title"); ?> <?php print("$page_title"); ?></title>

Posted

Yeah, I see how that works. That's actually pretty much how my site is set up now, having each page pull in menu and footer includes, except that the header info is hard-coded. That would just make the header dynamic as well. Not a bad idea at all.

 

However, it seems a bit cleaner to have one template with all the structure calling in content-only files. I know, the way it is there's very minimal structure in the content files, but still, it has calls repeated in each file. What if I want to change the calls? I'd have to do it for each file. It would be so nice if the content files had nothing in them except the content (duh) and a few variable definitions for page title, description, stylesheet, etc. Maybe there's a way to tell PHP to look for the variable definition(s) in the content files?

 

On the other hand, at some point I'm going to try to hook all this up to a database. Then the definitions will live in databaseland and none of this will matter. But the DB hookup is a level of complexity I'm not ready to tackle just yet.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...