Presentation is loading. Please wait.

Presentation is loading. Please wait.

23 November 2015 Scheiden van HTML en PHP emplatePower.

Similar presentations


Presentation on theme: "23 November 2015 Scheiden van HTML en PHP emplatePower."— Presentation transcript:

1 23 November 2015 Scheiden van HTML en PHP emplatePower

2 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.

3 Staat los van aanroepende php-script

4 templatePower (wat is niet goed?)

5

6 templatePower (FORMS) form.tpl The following errors occurred. - {message} Email:

7 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[‘email’]) ) { $errorMessage[] = 'No emailaddress 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(); ?>

8 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

9 templatePower IMAGES

10 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(); ?>

11 templatePower (ASSIGN(ARRAY)) {titel} Welkom {persoon}! prepare(); $tpl->assign(array( "titel" => "TemplatePower", “persoon" => "XenoX“ ) ); $tpl->printToScreen(); ?>

12 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

13 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(); ?>

14 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(); ?>

15 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(); ?>

16 templatePower (BLOCK) {titel} Welkom {persoon}! {id} : {naam}

17 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(); ?>

18 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(); ?>

19 templatePower (OUTPUT VAR) prepare(); $tpl->assign(array("titel" => "TemplatePower“, "persoon" => "XenoX“ )); $template = $tpl->getOutputContent(); ?>

20 templatePower (FORMS) form.tpl The following errors occurred. - {message} Email:

21 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($email == '') { $errorMessage[] = 'No emailadress 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(); ?>


Download ppt "23 November 2015 Scheiden van HTML en PHP emplatePower."

Similar presentations


Ads by Google