1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development return file as html document

Discussion in 'Software' started by Liquid K9, 5 May 2005.

  1. Liquid K9

    Liquid K9 Human programmer.. heh

    Joined:
    1 Sep 2002
    Posts:
    3,111
    Likes Received:
    2
    PHP:
        // get html body of web page
        
    function importTemplate$filepath )
        {
            
    // get source documents
            
    $src_temp file_get_contents"script/server/template.php" );
            
    $src_body file_get_contents$filepath );
            
            
    // get html body
            
    $begin_pos strpos$src_temp "<body>" ) + 6;
            
    $end_pos strpos$src_temp "</body>" );
            
    $html substr$src_temp $begin_pos , ($end_pos-$begin_pos) );
            
            
    // return stream to browser
            
    header('Content-Type: text/html; charset=ISO-8859-4');
            echo 
    $html;
        }
    What I'm trying to do, is have a site-wide template document and a body definition document. In the bodydef file, you create the webpage in the body area as usual and include a php script. The idea is that when the user visits the bodydef file, it replaces the body of the template file with the body of the bodydef file and sends that back to the browser as a html page. The purpose of all this being to be able to have a site-wide template; the basic outlay of the website - the navigation, the banner logo etc. is the same everywhere (prevents inconsistency).

    my problem here is, I can create a html document as you can see from the php code, however I dont know how to send back the $html variable as a html document. Basically, I want to create a custom html document and send it back to the browser as a webpage; I dont want the page which the user visits to be visible to them,only the custom authored html that is sent back to the browser.

    sorry, if its a bit confusing.
     
  2. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    i don't see why echo importtemplate(..); wouldn't work?
     
  3. [Jonny]

    [Jonny] What's a Dremel?

    Joined:
    1 Sep 2003
    Posts:
    296
    Likes Received:
    0
    Yeah, but I think it has to be "return $html;" rather than "echo $html;" in the function if he is going to use that.
     
  4. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    no, i mean't outside of the function. Its bad practise to output within a function other than 'return'ing it
     
  5. Liquid K9

    Liquid K9 Human programmer.. heh

    Joined:
    1 Sep 2002
    Posts:
    3,111
    Likes Received:
    2
    well, the problem to begin with is that the code is incomplete. As it is, it only grabs the body of the bodydef file and does not place that in the template file.

    to do that, the code must be:
    PHP:
        // get source documents
        
    $src_temp file_get_contents"script/server/template.php" );
        
    $src_body file_get_contents__FILE__ );
        
        
    // get html body
        
    $begin_pos strpos$src_temp "<body>" ) + 6;
        
    $end_pos strpos$src_temp "</body>" );
        
    $temp_html substr$src_temp $begin_pos , ($end_pos-$begin_pos);
        
    $html str_replace"<!--page_content-->" $temp_html $src_temp );
        
        
    // return stream to browser
        
    echo $html;
    the problem is that when the script is run, it returns both the output of the function AND the body of the page in the bodydef file.

    EDIT:
    maybe I didnt make this clear enough: I want to dynamically create a html document and return that to the browser and nothing else; if the script which returns that document to the browser contains html, that should be completely ignored; the dynamically created file should be sent only
     
    Last edited: 6 May 2005
  6. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    if you only want the echo $html to be displayed, then why not exit; after the function has been called?

    PHP:
    // get html body of web page
        
    function importTemplate$filepath )
        {
            
    // get source documents
            
    $src_temp file_get_contents"script/server/template.php" );
            
    $src_body file_get_contents$filepath );
            
            
    // get html body
            
    $begin_pos strpos$src_temp "<body>" ) + 6;
            
    $end_pos strpos$src_temp "</body>" );
            
    $html substr$src_temp $begin_pos , ($end_pos-$begin_pos) );
        
    $html str_replace"<!--page_content-->" $html $src_temp ); 
            
            
    // return stream to browser
            
    return $html;
        }

    //to run
    $complete importTemplate('filename')
    if (!empty(
    $complete)) { //if the returned value is NOT empty
    echo $complete;
    exit; 
    //terminates this script
    } else { //else, runs this script instead of displaying fetched html
    ...
    if you want to execute the code you load ($src_body) then try using eval.
     
  7. Liquid K9

    Liquid K9 Human programmer.. heh

    Joined:
    1 Sep 2002
    Posts:
    3,111
    Likes Received:
    2
    well, that works well apart from one problem: the body content is taken from the wrong part of the page.

    At the moment, it shows a section of the php script instead of the body of the page.
     
  8. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    so $src_body = eval($src_body);

    but if the $src_body file is 'this' file then it will make an infinite loop?
     
  9. Liquid K9

    Liquid K9 Human programmer.. heh

    Joined:
    1 Sep 2002
    Posts:
    3,111
    Likes Received:
    2
    It appears that the code works near-perfectly when placed at the end of a bodydef file. The html body is shown correctly, however it is shown twice. I wonder why this is?
     

Share This Page