Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Two html pages and a php driver

    Hi,

    First post, searched a bit but didn't know how to phrase it, so I came up empty.

    I have two files and a php driver page. I want to combine the two files with the driver page. Both files are html.

    Basically what I am doing is using one html file as a template and the other page is used as the content. Depending on the variable passed in the link, the corresponding content page will be served as the content.

    I also have another scenario where I would just like to have a template page with some php code in the content section that grabs another html page and puts that information into that section.

    All of these file reside on the same server.

  • #2
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is what I came up with. This snippet works, but, I'm sure there's a lot of overhead here, especially when I start adding more content and the file sizes start getting larger. Please advise.

    content.html
    Code:
    <b> Hello this content management system works! </b>
    template.html
    Code:
    <html>
    <head>
    <title>This is a template test</title>
    </head>
    <body>
    {content}
    <hr><hr>
    {content2}
    </body>
    </html>
    content2.html
    Code:
    <font color = 'blue'>Yes it works quite well</font>
    driver.php
    Code:
    <?php
    
    
    $template = "template.html";
    $str = implode('', file( $template)) ;
    
    $content1 = fopen("content.html", "r");
    $info1 = ''; 
    while  (!feof($content1)) 
    { 
        $info1  .=  fgets($content1);     
    } 
    
    $content2 = fopen("content2.html", "r");
    $info2 = ''; 
    while  (!feof($content2)) 
    { 
        $info2  .=  fgets($content2);     
    } 
    
    $chunk = explode("{content}" , $str);
    
    
    $chunk2 = explode("{content2}", $chunk[1]);
    
    print $chunk[0];
    print $info1;
    print $chunk2[0];
    print $info2;
    print $chunk2[1];
    
    ?>
    to see the results, go to www.extremausa.com/test/driver.php

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glad you got it working, but I think there should be a better way to do this...

    For your current simple page an include would do;

    Code:
    <html>
      <head>
        <title>Title</title>
      </head>
    
      <body>
      <?php include("content.html"); ?>
      <hr /><hr />
      <?php include("content2.html"); ?>
      </body>
    </html>

  • #4
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes that works well, and I did not think of that simple solution!

    I figured out another solution that still lets you have a template, content, and driver page. This is a lot simpler than what I had.

    The next thing that I am going to work on is putting this into a function so you can feed it an array of filenames with their corresponding keys.

    Code:
    <?php
    
    $str = implode('', file("template.html"));
    
    $content1str = implode('', file("content.html"));
    
    $content2str = implode('', file("content2.html"));
    
    $str = str_replace("{content}" , $content1str, $str);
    $str = str_replace("{content2}" , $content2str, $str);
    
    print $str;
    
    ?>

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Completed function looks like this. It's overloaded to take both files that will be converted to string variables in the function and it will also take plain string variables. Enjoy!

    Code:
    <?php
    
    function templatedriver($templatefilename, $vararray){
    	
    	$str = implode('', file($templatefilename));
    	// Changes template to string
    	
    	foreach($vararray as $block=>$content) {
    		
    		if(is_file($content)){
    			$contentstr = implode('', file($content)); 
    			// Changes file content to string
    		}
    		else if(is_string($content)){
    			$contentstr = $content;
    		}
    		else {
    			die("This is not compatible with the templatedriver function.... yet!");
    		}
    		
    		$str = str_replace($block, $contentstr, $str);
    	} // end foreach
    	
    	return $str;
    }// This function returns a string with the replaced content
    
    print templatedriver("template.html",array(
    						'{content}' => 'content.html',
    						'{content2}' => 'Ahah! this is overloaded!')
    );
    
    // template.html should be replaced by your template,
    /*
    *	In the above example, the bracketed part is what will be replaced
    *	in your template page. The filename or variable is what will replace it.
    *	
    *	You can have as many blocks of content that you want, this only
    *	works on files that reside on your server. Offsite files may or may
    *	not work depending on your and their configuration.
    */
    		
    		
    ?>
    Last edited by polyhedron; 04-19-2004 at 10:14 PM.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm, still seems a bit lengthly... Includes would be fine however the other methods would work... include is much faster though.

    Another tip, you can use ob_start(); and other output buffers to store includes as variables rather than having them added to the page, but for what you want... Includes should be fine, as this is also the fastest solution.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wouldn't use the "die()" to abort the template driver for unknown variable content. If you have 100 template variables and one of them is corrupted/wrong, the user might prefer to see the most of the file with only one part missing than looking at a very verbose error message that doesn't tell him anyhing. Templates aren't that crucial IMHO that it would justify to halt the execution completely upon an error.

    Better: Use trigger_error() to throw a PHP error. These errors can be logged and tailored to suit your needs.
    De gustibus non est disputandum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •