|
Posted by William Wilcox on 10/22/05 00:37
I've stumbled around on this for the past three days, and I'm hoping
someone here can give me some insight. I put together project just to
learn a bit of php and had some success, so now I'm putting together
something quite a bit more ambitious.
What I'm trying to do: Have one main script, which depending on the
variables passed to it, generates different content in HTML. I want 3
functions, one to generate the HTML header, one to generate the body and
one to generate the footer. What's returned from all three functions
should be put into one document and passed back to the browser.
The problem is the functions work by themselves, but the output isn't
being passed back to the main script or to the browser.
Here's the function code:
<?php
require_once "HTML/Template/IT.php";
require_once "DB.php";
require "db.inc";
// Logic to decide what the title of the page should be.
// Check to see if there is an article id in the page request.
$id = $_GET['id'];
if ( !empty( $id ))
{
// Open a connection to the database
$connection = DB::connect($dsn);
// Give me an error if something is wrong
if (DB::isError($connection))
ShowError($connection);
// Put together the query to get the title of the article.
$query = "SELECT artTitle FROM article WHERE artID = '$id'";
// Get the result from the database.
$result = $connection->query($query);
// Show an error message if something goes wrong.
if (DB::isError($result))
ShowError($result);
// Otherwise, set the title variable
$title=$result;
// Disconnect from the database
$connection->disconnect();
}
else
{
// for now this is set statically. Eventually it should be read
// from an xml site config file.
$title="Default Page Title";
}
// Put the header template together.
$template = new HTML_Template_IT("./templates");
$template->loadTemplatefile("header.tpl", true, true);
// Work with the title block.
$template->setCurrentBlock("TITLEVAR");
// Assign the title variable
$template->setVariable("TITLE", $title);
// Parse the block
$template->parseCurrentBlock();
// Output the HTML
$template->show();
?>
I think I may be making a mountain out of a mole hill. The reason I
want to break up the header, body and footer is that the content in each
can change quite dramatically. So I want to be able to generate just
the HTML for a particular object, and then composite all the objects
into one page. I don't want to produce a separate template for each
combination of objects. Just one template, and then each object going
into the page has a method which specifies how to display itself.
My normal google research isn't helping me, but that may be because I
don't know what I'm really searching for.
Thanks in advance.
Bill
Navigation:
[Reply to this message]
|