Forum Moderators: coopster
I'm trying to build this small template system...but I keep getting an error. The links in menu.php point to $page.html. But the problem seems to lie in the require_once ("$page.html"); - This is not allowed. How come? This is the error:
Fatal error: Failed opening required '.html' (include_path='.;c:\php4\pear') in C:\FoxServ\www\Other\template.php on line 12
<div class="body">
<?php require_once ("$page.html");?>
</div>
<div class="menu">
<?php require_once ("menu.php");?>
</div>
Thanks for any help!
Maybe $page is set in menu.php? If you can find where $page is set then you can either pass that variable or declare it in your main file, if that makes more sense. Can you find $page set to anything anywhere?
If the host page is template.php. What would be the best way to set that variable, say I want home.html to be the default. Would I use a header redirect or just set $page to "home" somewhere on the page.
Is this even a good method for building a template? I've heard it's bad programming technique to use variables like I have in a require_once.
$copy = a data source; //pulled from db or other source. Contains variable defs for require statements
require_once("config.php"); //may contain variable defs as flags or literal defintions
require_once("header.php");
require_once("mainMenu.php");
require_once("subMenu.php");
echo $copy.php;
require_once("footer.php");
Many (most) of my requires have variables in them, but I can not think of an example where I have used a variable as a require.
This may be way off base, since it is hard to see exactly what you are trying to do or what your program flow is supposed to look like.
WBF
<div class="body">
<?php require_once ("$page.html");?>
</div>
<div class="menu">
<?php require_once ("menu.php");?>
</div>
menu.php stays static while $page.html brings in the content pages (services, contact, work, etc...) via menu.php.
That's it...just a simple website template.
I know it may not be the best way to go about this but I'm just trying to get this example to work so I know atleast one way to do it.
If I am following you, $page is being pulled from a db or other source based on the link on the menu.
You will need to declare $page as was noted earlier.
I am not sure that the require_once statement is the best way to do this.
If $page is coming from a db you will essentially construct $page on the fly.
If it is a static document, meaning it sits as its own independent doc on your server, use your requires on that doc. I have many pages in which I simply cut and past the copy from a word processor into a template similar to what I outlined in my first post, and then save under the appropiate name, e.g. "services", "contact", etc.
On the other hand I have pages in which the content is pulled from a db and inserted into the template on the fly with echo statements.
<?
/*db routine here
including declaration of variables*/
require_once("header.php");
require_once("mainMenu.php");
require_once("subMenu.php");
/*echo statements here - may flip between html and php, e.g.
?>
<div>
<h1><?echo $title;?></h1>
<?echo $content;?>
</div>
?>
and so on...*/
require_once("footer.php");
?>
WBF
Ok, so $page.html needs a default page upon page load.
I'm not pulling these pages out of a db yet, I want to.
You wrote:
<?
/*db routine here
including declaration of variables*/
require_once("header.php");
require_once("mainMenu.php");
require_once("subMenu.php");
/*echo statements here - may flip between html and php, e.g.
?>
<div>
<h1><?echo $title;?></h1>
<?echo $content;?>
</div>
?>
and so on...*/
require_once("footer.php");
?>
So header.php, mainmenu.php, submenu.php and footer.php establish your page. $content comes from a database table- The $content variable has to keep changing with new information from page to page right?
Two questions: How do you fill that $content variable with default information...when the user just gets to the site for the first time? I think that'll help me with my problem somehow. Also, does all the site content lie in the same db table, or would you generally use a separate content table for each individual page (services, contact, etc...)
Exactly. Everything that remains constant is defined in the require statements. That way one change in a require statement will appear, as if by magic, across every page in the site.
"Two questions: How do you fill that $content variable with default information...when the user just gets to the site for the first time?"
Just lay your content in between the requires (at the approriate point of course). Cut and paste is the most direct route.
"Also, does all the site content lie in the same db table, or would you generally use a separate content table for each individual page (services, contact, etc...)"
All in one db table. You can arrange it as you see fit. Simplest would be title, content which gets echoed to <title>, <h1> $title </h1> and, <div> $content </div>, but you could include fields for other odds and ends like <description>, <keywords>, etc.
Usually if I am pulling from a db it is for dynamic data like membership lists, sales lists, etc. These things go into tabular data, lists, or even links to other dynamic pages. Those pages might include pictures and detailed descriptions.
So, I have one db with columns for things like productNumber, productName, headline, detailDescription, price, smallPic, frontPic, backPic, sidePic, testimonial, etc. Then I can creat a page with a small photo and headline description that links to a page with a detailed desciption with more photos. All of these db fields are simply echoed into the template page.
Your template is everything that doesn't change.
WBF