PDA

View Full Version : Template Customization Using PHP Objects


vinyl-junkie
02-01-2004, 09:44 AM
I have a problem that I don't think is quite covered in all the threads I've looked at, although this thread (http://www.phpdig.net/showthread.php?s=&threadid=339) looks like it has something close to what I'm looking for.

My web page header and footer are defined in a PHP class called Navigation. I pass a number of variables to the methods within the class, then have the following code that places header and footer, respectively, on the page:echo $Navigation->getHeader();

echo $Navigation->getFooter();

How can I incorporate a PHP object into my template so that I don't have to maintain it in two separate places, one for the rest of my site and the other for phpDig?

<added>
For clarification, my header includes everything from the <html> tag through the <body> tag.
</added>

Charter
02-05-2004, 06:49 AM
Hi. To use echo $Navigation->getHeader(); and echo $Navigation->getFooter(); perhaps try the following.

At the top of search_function.php add:

ob_start();
require("/path/to/header.php");
$my_header_file = ob_get_contents();
ob_end_clean();

ob_start();
require("/path/to/footer.php");
$my_footer_file = ob_get_contents();
ob_end_clean();

and then in search_function.php replace:

$t_mstrings = compact('powered_by_link','title_message','phpdig_version','result_message' ,'nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','n ext_link','templates_links');

with the following:

$t_mstrings = compact('my_header_file','my_footer_file','powered_by_link','title_message' ,'phpdig_version','result_message','nav_bar','ignore_message','ignore_comme ss','pages_bar','previous_link','next_link','templates_links');

Make the header.php and footer.php files contain echo $Navigation->getHeader(); and echo $Navigation->getFooter(); respectively, and then use <phpdig:my_header_file/> and <phpdig:my_footer_file/> in the template.

Remember to remove any "word" wrapping in the above code.

vinyl-junkie
02-05-2004, 07:58 PM
Something is missing, I think. Just to make sure I didn't have anything I overlooked in setting up the object oriented code, I added the following (in bold) to a section of code you told me to add:ob_start();
require("/home/napathon/public_html/ssi/header.php");
$my_header_file = ob_get_contents();
die($my_header_file);
ob_end_clean();
My header displayed as it should when I did the above. I also made sure that I had followed all your instructions in modifying the code, but when I added the code into the template, it didn't display my header and footer. Any ideas why?

Charter
02-06-2004, 09:17 AM
Hi. Change the following:

ob_start();
require("/path/to/header.php");
$my_header_file = ob_get_contents();
ob_end_clean();

to the following:

ob_start();
require("/path/to/header.php");
$my_header_file = ob_get_contents();
ob_end_clean();
echo $my_header_file;
exit();

and then call search.php to see if $my_header_file actually contains the header information.

vinyl-junkie
02-06-2004, 06:53 PM
Yes, the header displays just fine when I do that.

Charter
02-06-2004, 07:09 PM
Hi. In search_function.php the "ob" code is at the top, before the $t_mstrings = compact('my_header_file','my_footer_file',... part? Note that in $t_strings there are no dollar signs on the right hand side. Are you using a template that came with PhpDig, and <phpdig:my_header_file/> and <phpdig:my_footer_file/> are used in the template file? It seems like there may just be a typo somewhere as $my_header_file and $my_footer_file are holding content.

vinyl-junkie
02-06-2004, 07:53 PM
In search_function.php, the "ob" part is the very first statements in the code, and looks like this. ob_start();
require("/home/napathon/public_html/ssi/header.php");
$my_header_file = ob_get_contents();
ob_end_clean();

ob_start();
require("/home/napathon/public_html/ssi/footer.php");
$my_footer_file = ob_get_contents();
ob_end_clean();

The $t_mstrings statement is toward the bottom of the file, and looks like this:if ($template == 'array' || is_file($template)) {
$phpdig_version = PHPDIG_VERSION;
$t_mstrings = compact('my_header_file','my_footer_file','powered_by_link','title_message' ,'phpdig_version','result_message','nav_bar','ignore_message','ignore_comme ss','pages_bar','previous_link','next_link','templates_links');
Word wrapping was removed.

The config.php file has this: $template = "$relative_script_path/templates/template.php";
and that is the correct path and filename for it.

I took a phpDig template and modified it so that the header and footer have this, respectively:<phpdig:my_header_file/>

<phpdig:my_footer_file/>

Is there anything I missed?

Charter
02-06-2004, 08:11 PM
Hi. Try either modifying the .../PHP_DIG/templates/template.php file so that it contains the <phpdig:my_header_file/> and <phpdig:my_footer_file/> tags or setting the $template variable in the config.php file to the PhpDig template that uses the <phpdig:my_header_file/> and <phpdig:my_footer_file/> tags. It looks like one template has the tags but another template sans tags is being used by PhpDig.

vinyl-junkie
02-06-2004, 09:54 PM
I've messed with this for quite a while this evening, and I just don't see where the problem is. One thing I noticed is that the "else" condition of the following code is being executed, rather than the "if" condition. Is this what should happen?
if ($template == 'array' || is_file($template)) {
$phpdig_version = PHPDIG_VERSION;
$t_mstrings = compact('my_header_file','my_footer_file','powered_by_link','title_message' ,'phpdig_version','result_message','nav_bar','ignore_message','ignore_comme ss','pages_bar','previous_link','next_link','templates_links');
$t_fstrings = phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'templ ate',$template);
if ($template == 'array') {
return array_merge($t_mstrings,$t_fstrings,array('results'=>$table_results));
}
else {
$t_strings = array_merge($t_mstrings,$t_fstrings);
phpdigParseTemplate($template,$t_strings,$table_results);
}
}

Charter
02-06-2004, 11:22 PM
>> Is this what should happen?

Hi. That's okay. One thing I did notice is that if you call http://www.napathon.net/ssi/header.php and http://www.napathon.net/ssi/footer.php from the browser, the "call to a member function on a non-object" error appears.

vinyl-junkie
02-07-2004, 12:16 AM
Well, now I'm really confused. I have all the code in search.php that sets the value of the header and footer variables, instantiates the class, etc. The only thing that is in header.php is this.
<?php
echo $Navigation->getHeader();
?>
Have I completely misunderstood what I need to do in order to set this up?

Charter
02-07-2004, 12:26 AM
>> Have I completely misunderstood what I need to do in order to set this up?

Hi. Nah, I think that the header.php and footer.php files need to have access to the header-footer class/script so that echo $Navigation->getHeader(); and echo $Navigation->getFooter(); output the header and footer.

Perhaps add an include("/path/to/class/script.php"); into header.php and footer.php, and then call just header.php and footer.php from the browser to check and see if the HTML is onscreen.

vinyl-junkie
02-07-2004, 06:11 AM
Still no joy. This may be overkill but I've saved all the relevant files with .txt file extensions and uploaded them to the Web server into the same directory as their .php counterpart. Maybe this will help diagnose the problem.

http://www.napathon.net/PHP_DIG/search.txt

http://www.napathon.net/ssi/header.txt

http://www.napathon.net/ssi/footer.txt

http://www.napathon.net/PHP_DIG/includes/config.txt

http://www.napathon.net/PHP_DIG/libs/search_function.txt

http://www.napathon.net/PHP_DIG/templates/template.txt

Charter
02-07-2004, 09:36 AM
Hi. Change header.php back to the following:

<?php
echo $Navigation->getHeader();
?>

Change footer.php back to the following:

<?php
echo $Navigation->getFooter();
?>

Remove the following from search.php and stick it into a new file, say makeheadfoot.php located at /home/napathon/public_html/ssi/makeheadfoot.php:

/*
--------------------------------------------------------------------------------
My code goes here to incorporate my template
--------------------------------------------------------------------------------
*/
require_once '/home/napathon/public_html/class/Navigation.php';

// This goes into the meta name="description" content field
$MetaDescription = "Build your own website search engine using phpDig.";

// This goes into the meta name="keywords" content field
$MetaKeywords = "website search engine";

// This goes into the page title
$PageTitle = "Search My Website";

// This goes into the table title
$TableTitle = $PageTitle;

// Robots meta tag. Leave this as null if you don't intend to use it
$Robots = "Index, Follow";

// Instantiate the Navigation class; then build the page header and footer
$Navigation = new Navigation();
$Navigation->addHeader($HTMLTitle, $MetaDescription, $MetaKeywords, $PageTitle, $Robots);
$Navigation->addFooter();

In search_function.php move the following code to right above $timer = new phpdigTimer('html'); but inside the function:

ob_start();
require("/home/napathon/public_html/ssi/header.php");
$my_header_file = ob_get_contents();
ob_end_clean();

ob_start();
require("/home/napathon/public_html/ssi/footer.php");
$my_footer_file = ob_get_contents();
ob_end_clean();

Also in search_function.php right before the first ob_start(); call but still inside the function add the following:

require("/home/napathon/public_html/ssi/makeheadfoot.php");

"Inside the function" means the following:

function phpdigSearch($id_connect, $query_string, $option='start', $refine=0,
$refine_url='', $lim_start=0, $limite=10, $browse=0,
$site=0, $path='', $relative_script_path = '.', $template='',
$templates_links='')
{
///////////////// stuff goes here /////////////////
$timer = new phpdigTimer('html');
$timer->start('All');

Keep the template, $t_mstrings variable, and config file the same and give it a whirl.

vinyl-junkie
02-07-2004, 10:07 AM
It worked! :D Thanks so much for sticking with me until we got this problem solved. I just have some minor formatting issues to deal with now, but that's ok. They're only CSS things, and I can handle that. It's just so much nicer maintaining my template in one place.

Thanks again.

vinyl-junkie
02-07-2004, 08:00 PM
Just wanted to let everyone know that I have refreshed the .txt files that I posted earlier. They now have the correct code in them for utilitzing PHP objects for your page template. The new file that Charter suggested is in this link:

http://www.napathon.net/ssi/makeheadfoot.txt

I plan on leaving them there for a while to help anyone who comes along later and wants to do the same thing.

Hope you find it useful. :)

vinyl-junkie
03-19-2004, 07:04 PM
Just wanted to let everyone know that I have deleted from my server all the .txt files I had posted as part of this thread. The solution to this problem is a little hard to follow from this thread. Please read this thread (http://www.phpdig.net/showthread.php?s=&threadid=680&highlight=makeheadfoot.php) instead for a better explanation of the solution. Thanks.