23 November 2015 Scheiden van HTML en PHP emplatePower
templatePower Hoe halen we PHP en HTML uit elkaar Templates hebben één heel mooi doel, namelijk om PHP en HTML te scheiden. Zonder templates komt het vaak voor dat je een heel groot script hebt gemaakt, met HTML ertussen en dat je achteraf iets wil aanpassen aan het uiterlijk.
Staat los van aanroepende php-script
templatePower (wat is niet goed?)
templatePower (FORMS) form.tpl The following errors occurred. - {message}
templatePower (FORMS) myscript.php <?php include_once('./class.TemplatePower.inc.php'); $tpl = new TemplatePower('form.tpl'); $tpl->prepare(); $errorMessage = Array(); $errorFound = false; if( isset( $_POST[‘submit’] ) ) { if ( !isset($_POST[‘ ’]) ) { $errorMessage[] = 'No address entered'; $errorFound = true; } if( $errorFound ) { $tpl->newBlock('error'); $size = sizeof($errorMessage); for( $i=0; $i < $size; $i++ ) { $tpl->newBlock('message'); $tpl->assign('message', $errorMessage[$i]); } } else { Header('Location: member.php'); } } $tpl->printToScreen(); ?>
templatePower (ASSIGNGLOBAL) assignGlobal ( string variablename, mixed value ) assignGlobal ( array( variablename => value ) ) myscript.php <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower( "./img.tpl" ); $tpl->prepare(); $tpl->assignGlobal( "imagedir", "images"); for ( $i=1; $i<=10; $i++ ) { $tpl->newBlock( "image" ); $tpl->assign( "id", $i ); } $tpl->printToScreen(); ?> img.tpl AssignGlobal Example
templatePower IMAGES
templatePower (ASSIGN) BestandIndex.tpl index Welkom {persoon}! <?php include("class.TemplatePowe r.inc.php"); $tpl = new TemplatePower("index.tpl"); $tpl->prepare(); $tpl->assign("persoon", "XenoX"); $tpl->printToScreen(); ?>
templatePower (ASSIGN(ARRAY)) {titel} Welkom {persoon}! prepare(); $tpl->assign(array( "titel" => "TemplatePower", “persoon" => "XenoX“ ) ); $tpl->printToScreen(); ?>
templatePower (ASSIGNINCLUDE) myscript.php <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower( "./include.tpl" ); $tpl->assignInclude( "header", "./header.tpl" ); $tpl->assignInclude( "content", "./about.php" ); $tpl->prepare(); $tpl->printToScreen(); ?> include.tpl AssignInclude Example
templatePower (ASSIGNINCLUDE) <?php include( "./class.TemplatePower.inc.php"); //connect to database $link = mysql_connect("host", "user", "passwd") or die("Could not connect"); mysql_select_db("my_database") or die("Could not select database"); //get database templates $qry = "SELECT base, header FROM templates"; $result = mysql_query($qry); if( mysql_num_rows($result) > 0) { list($base, $header) = mysql_fetch_row($result); } //make a new TemplatePower object $tpl = new TemplatePower( $base, T_BYVAR ); //assign include template by variable $tpl->assignInclude( "header", $header, T_BYVAR ); $tpl->prepare(); //print the result $tpl->printToScreen(); ?>
templatePower (GETVARVALUE) number.tpl AssignInclude Example {number} {total} myscript.php <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower( "./number.tpl" ); $tpl->prepare(); for( $i=1; $i <= 10; $i++ ) { $tpl->newBlock( "number" ); $tpl->assign( "number", $i ); $tpl->assign( "_ROOT.total", ($tpl->getVarValue( "_ROOT.total" ) + $i) ); } $tpl->printToScreen(); ?>
templatePower (GOTOBLOCK) newBlock.tpl NewBlock Names {name} {total_names} myscript.php <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower( "./newBlock.tpl" ); $tpl->prepare(); $count = 0; while( $count < 10 ) { $tpl->newBlock( "name_row" ); $tpl->assign( "name", "Ron" ); $count++; } $tpl->gotoBlock( "_ROOT" ); $tpl->assign( "total_names", $count ); $tpl->printToScreen(); ?>
templatePower (BLOCK) {titel} Welkom {persoon}! {id} : {naam}
templatePower (BLOCK) prepare(); $tpl->assign(array("titel" => "TemplatePower", "persoon" => "XenoX“ )); $tpl->newBlock("rij"); $tpl->assign(array("id" => "1", "naam" => "Joël“ )); $tpl->newBlock("rij"); $tpl->assign(array("id" => "11", "naam" => "FangorN“ )); $tpl->printToScreen(); ?>
templatePower (BLOCK-NESTED) prepare(); $tpl->assign(array("titel" => "TemplatePower", "persoon" => "XenoX“ )); $tpl->newBlock("rij"); $tpl->assign(array("id" => "25", "naam" => "XenoX“ )); $tpl->newBlock("blockinblock"); $tpl->printToScreen(); ?>
templatePower (OUTPUT VAR) prepare(); $tpl->assign(array("titel" => "TemplatePower“, "persoon" => "XenoX“ )); $template = $tpl->getOutputContent(); ?>
templatePower (FORMS) form.tpl The following errors occurred. - {message}
templatePower (FORMS) myscript.php <?php include_once('./class.TemplatePower.inc.php'); $tpl = new TemplatePower('form.tpl'); $tpl->prepare(); $errorMessage = Array(); $errorFound = false; if( isset( $submit ) ) { if($ == '') { $errorMessage[] = 'No adress entered'; $errorFound = true; } if( $errorFound ) { $tpl->newBlock('error'); $size = sizeof($errorMessage); for( $i=0; $i < $size; $i++ ) { $tpl->newBlock('message'); $tpl->assign('message', $errorMessage[$i]); } } else { Header('Location: member.php'); } } $tpl->printToScreen(); ?>