PhpDig.net

PhpDig.net (http://www.phpdig.net/forum/index.php)
-   How-to Forum (http://www.phpdig.net/forum/forumdisplay.php?f=33)
-   -   PHP includes in templates (http://www.phpdig.net/forum/showthread.php?t=348)

gman 01-02-2004 11:40 AM

PHP includes in templates
 
I have read through the forums, but still need help using PHP includes in the templates. Can someone please give detailed instructions on how to do this with some examples.

Maybe, somehow show how to include a header include and a footer include in the template file.

I would love to use this excellent app... just need a little help to do it.

Charter 01-02-2004 08:18 PM

Hi. Say you want to include header.html and footer.html in a template.

First add the following function to the top of the search_function.php file:
PHP Code:

function get_my_includes($filename) {
  if (
file_exists($filename)) {
    
$buffer "";
    
$handle fopen($filename"r");
    while (!
feof($handle)) {
      
$buffer .= fgets($handle4096);
    }
    
fclose($handle);
    return 
$buffer;
  }


Next in search_function.php replace:
PHP Code:

if ($template == 'array' || is_file($template)) {
    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings compact('powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    
$t_fstrings phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$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);
    }


with the following:
PHP Code:

// check that the paths are correct or use full paths
$my_page_header get_my_includes("../../dir/header.html");
$my_page_footer get_my_includes("../../dir/footer.html");

if (
$template == 'array' || is_file($template)) {
    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings compact('my_page_header','my_page_footer','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    
$t_fstrings phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$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);
    }


Finally in the template use:
Code:

<phpdig:my_page_header/>
<phpdig:my_page_footer/>

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

gman 01-05-2004 05:12 AM

Wow, thanks for the in-depth reply. Exactly the response I was looking for. I think this will help a lot of people out including myself.

gman 01-05-2004 02:03 PM

By the way, it worked like a charm. Thanks again.

gomac 04-01-2004 06:52 PM

RE: PHP includes in templates
 
This is brilliant! Any way to modify this so that it can include AND parse any PHP in the include?

Charter 04-01-2004 07:01 PM

Hi. Using the same method as above, you could include header.php, sidebar.php, and footer.php in a template, or you could also make your own custom PHP search page as follows.

In the search.php file do the following:
PHP Code:

$arrayout phpdigSearch($id_connect$query_string$option$refine,
                       
$refine_url$lim_start$limite$browse,
                       
$site$path$relative_script_path'array');

if (
is_file("custom_search.php")) {
    include 
"custom_search.php";
} else { exit(); } 

Save the following in the same directory as the search.php file:
PHP Code:

<?php
// custom_search.php

if (eregi("custom_search.php",$_SERVER['SCRIPT_FILENAME']) || 
    
eregi("custom_search.php",$_SERVER['REQUEST_URI'])) {
    exit();
}

echo 
"<html><body>";

echo 
$arrayout['result_message'],
     
$arrayout['powered_by_link'],
     
$arrayout['title_message'],
     
$arrayout['phpdig_version'],
     
$arrayout['nav_bar'],
     
$arrayout['pages_bar'],
     
$arrayout['next_link'],
     
$arrayout['form_head'],
     
$arrayout['form_title'],
     
$arrayout['form_field'],
     
$arrayout['form_select'],
     
$arrayout['form_button'],
     
$arrayout['form_radio'],
     
$arrayout['form_foot'];

if (!empty(
$arrayout['results'])) {
    
$num_out count($arrayout['results']);
} else { 
$num_out 0; }
$num_start $lim_start 1;
$num_end $lim_start $num_out;

for (
$i=$num_start$i<=$num_end$i++) {
     echo 
$i.". ";
     
$arrayout2 $arrayout['results'][$i];
     echo 
$arrayout2['weight'],
          
$arrayout2['img_tag'],
          
$arrayout2['page_link'],
          
$arrayout2['limit_links'],
          
$arrayout2['filesize'],
          
$arrayout2['update_date'],
          
$arrayout2['complete_path'],
          
$arrayout2['link_title'],
          
$arrayout2['text'];
     echo 
"<br><br>";
}

echo 
"</body></html>";

?>

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

jclabaugh 06-03-2004 12:33 PM

Charter:

I followed your code above, modifying search_function.php and the template. I included a header and footer file.

My scripts
search_function.php
Code:

I added to the top:
function get_my_includes($filename) {
  if (file_exists($filename)) {
  $buffer = "";
  $handle = fopen($filename, "r");
  while (!feof($handle)) {
    $buffer .= fgets($handle, 4096);
  }
  fclose ($handle);
  return $buffer;
 }
}

I modified the later bits as such.
Code:

$my_page_header = get_my_includes("./includes/header_aux.inc.php");
$my_page_footer = get_my_includes("./includes/footer_aux.inc.php");

if ($template == 'array' || is_file($template)) {
    $phpdig_version = PHPDIG_VERSION;
    $t_mstrings = compact('my_page_header','my_page_footer','powered_by_link','title_message','phpdig_version','result_message','nav_bar','ignore_message','ignore_commess','pages_bar','previous_link','next_link','templates_links');
    $t_fstrings = phpdigMakeForm($query_string,$option,$limite,SEARCH_PAGE,$site,$path,'template',$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);
    }
}

And to the template I added:
Code:

<phpdig:my_page_header/>
<phpdig:my_page_footer/>

No problems, except the included files aren't being parsed, just included as code. Any ideas on how to get these included files to parse?

Cheers,

JC

vinyl-junkie 06-03-2004 05:54 PM

Quote:

Originally posted by jclabaugh

No problems, except the included files aren't being parsed, just included as code. Any ideas on how to get these included files to parse?

Did you see this thread? This method will parse your files. If you have any questions about it, I'll be glad to help. :)

<edit>
BTW, I forgot to welcome you to the forum. Glad you could join us! :D
</edit>

Charter 06-04-2004 04:50 AM

Hi. To parse PHP files, remove the get_my_includes function, as that reads but does not parse PHP files, and then replace:
PHP Code:

$my_page_header get_my_includes("./includes/header_aux.inc.php");
$my_page_footer get_my_includes("./includes/footer_aux.inc.php"); 

with the following:
PHP Code:

$my_page_header = include("/path/to/includes/header_aux.inc.php");
$my_page_footer = include("/path/to/includes/footer_aux.inc.php"); 

Then just keep the rest the same.

jclabaugh 06-04-2004 07:39 AM

Ok, Pat and Charter, I have tried both of your code examples. Thanks for the advice but I am still not having success... I decided to go with Charter's example for simplicity. I removed the get_my_includes function and changed the other code in search_function.php to

Code:

$my_header_file = include("./includes/header_search.inc.php");
$my_footer_file = include("./includes/footer_search.inc.php");

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_commess','pages_bar','previous_link','next_link','templates_links');

and now it parses the PHP of my includes. Great, but though the structure of the template is still

Code:

<phpdig:my_header_file/>
normal phpdig code here
<phpdig:my_footer_file/>

it prints out both the header and footer before the phpdig search stuff. I have changed around the order of the strings being called in 'compact' above (putting 'my_footer_file' after 'template_links') but it made no difference.

Any other ideas on how I can get it to print a header then the phpdig content then the footer, in that order?

Cheers,

JC

Charter 06-04-2004 07:45 AM

Hi. Try taking a look at the HTML source from your page and then in the template just move <phpdig:my_header_file/> and <phpdig:my_footer_file/> so that the order is correct.

jclabaugh 06-04-2004 08:16 AM

Charter, thanks. There appeared to be no way to reorder the way they are listed to provide the desired result, given the order is
Code:

<phpdig:my_header_file/>
normal phpdig code here
<phpdig:my_footer_file/>

moving the footer up or the header down in relation to the phpdig code block made no difference.

I'm on a time limit so I gave up and hardwired the HTML into the template and got the result I 'wanted'. Thanks for your help.

subscript 08-12-2004 09:32 AM

I was having same problem as jclabaugh - order of template header and footer.

Found that this ordering is not adjusted in the HTML template file, it must be adjusted via the order of elements appearing in /libs/search_function.php

Code should read something like this to place footer below PHPDig search results:

PHP Code:

if ($template == 'array' || is_file($template)) {

// use full paths to header
$my_page_header = include("../../dir/search_header.php");

    
$phpdig_version PHPDIG_VERSION;
    
$t_mstrings =  compact('my_page_header','my_page_footer','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);
    }

// use full paths footer
$my_page_footer = include("../../dir/search_footer.php");

}
else { ... 


vinyl-junkie 08-12-2004 05:17 PM

Welcome to the forum, subscript. :D

Your method of including a custom header/footer is certainly a pretty simple way of doing it. Thanks for sharing. It's nice to have someone post one more way of doing it. :)

Charter 12-03-2004 10:02 PM

Also, from php.net:

return.php
PHP Code:

<?php

$var 
'PHP';

return 
$var;

?>

noreturn.php
PHP Code:

<?php

$var 
'PHP';

?>

testreturns.php
PHP Code:

<?php

$foo 
= include 'return.php';

echo 
$foo// prints 'PHP'

$bar = include 'noreturn.php';

echo 
$bar// prints 1

?>

So if you structure the included PHP files using return then you should be without order problems and without seeing ones on your pages.


All times are GMT -8. The time now is 03:31 AM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.