Php Variables

liunx

Guest
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 <i>after</i> it's been called.<br /><br />Is there any way to do what I want to do with a little bit of trickery? Or fail that, by whatever means?<br /><br />Thanks.<!--content-->
I do it the other way<br />My page defines the title and then includes the header<br />which can then display the title variable.<br /><br />my template for a page<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />// add header and additional title<br />$page_title="home";<br />include $_SERVER['DOCUMENT_ROOT']."/header.php";<br />// begin main content<br />?><br /><br /><p>Content</p><br /><br /><?php<br />// end main content <br />// add footer file <br />include $_SERVER['DOCUMENT_ROOT']."/footer.php";<br />?><!--c2--></div><!--ec2--><br /><br />then in the header.php<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><HEAD><br /><title><?php print("$site_title"); ?> <?php print("$page_title"); ?></title><!--c2--></div><!--ec2--><!--content-->
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.<br /><br />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?<br /><br />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.<!--content-->
 
Back
Top