::: T4VN Statistics :::
PHP Scripts : 64
PHP Example : 67
PHP Tutorials : 21
PHP News : 93
Total Coupon : 36
Other Tutorials :
Member : 215
Visitor Online : 3
Today Visit: 267
Total Visitor : 301289
Most Online : 41
Template Class
Author : YamahaBrez
Opens a file for a template, then replaces specified content in an array, and it can either print it to a static page, or you can use the "echo" command to print it dynamically.
Add multiple items to the array using the "replace" option and it will replace those as well with desired content added with the content array.
PHP Example : <?php
# Template Class -> derek
class template {
var $create = false ;
var $replace = array( "%content%" );
var $content = array( "Nothing for content input." );
var $tpl = "design.html" ;
function generate () {
$template = $this -> open ( $this -> tpl );
for ( $i = 0 ; $i < count ( $this -> replace ); $i ++) {
$template = str_replace ( $this -> replace [ $i ], $this -> content [ $i ], $template );
}
if ( $this -> create != false ) {
if ( is_file ( $this -> create )) {
@ chmod ( $this -> create , 0777 );
@ unlink ( $this -> create ) or die( "Unable to delete file to replace. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
}
$write_file = @ fopen ( $this -> create , "w" ) or die( "Failed to open file for writing. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
@ fwrite ( $write_file , $template ) or die( "Failed to generate static HTML file. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
@ fclose ( $write_file ) or die( "Failed to close static HTML file. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
@ chmod ( $this -> create , 0555 ) or die( "Failed to set permissions for file. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
# Log it
$log_location = sitedir . "logs/" . y . "_" . m . "_" . d . ".txt" ;
@ chmod ( $log_location , 0666 );
$fp = @ fopen ( $log_location , "a+" );
@ fwrite ( $fp , "page created -->" . ip . "-" . cpu . " | {$this->create} | " . fulldate . " | " . fulltime . "\n" );
@ fclose ( $fp );
@ chmod ( $log_location , 0444 );
return( true );
} else {
return( $template );
}
}
function open ( $file ) {
$tpl = sitedir . "includes/templates/" . $file ;
$template_open = @ fopen ( $tpl , "r" );
$template = fread ( $template_open , filesize ( $tpl )) or die( "Failed to read template {$tpl}. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
@ fclose ( $template_open ) or die( "Failed to close template. <br>Line: <strong>" . __LINE__ . "</strong><br>File: <strong>" . __FILE__ . "</strong>" );
return( $template );
}
}
?>
Usage Example :
PHP Example : <?
$tpl = new template ;
$tpl -> content = array( "This would replace %content%" );
echo $tpl -> generate ();
or
$tpl = new template ;
$tpl -> content = array( "This would replace %content%" );
$tpl -> create = "index.html" ;
$tpl -> generate ();
?>
Other Example
BBCode Using MySQL
::: Resources :::
::: New Templates :::
::: Other Tutorials :::