templatePower (FORMS) myscript.php

advertisement
Scheiden van
HTML en PHP
emplatePower
23 November 2015
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 (wat is niet goed?)
templatePower (FORMS)
form.tpl
<html> <head> <title></title> </head> <body>
<!-- START BLOCK : error --> The following errors occurred.<br>
<!-- START BLOCK : message --> - {message}<br>
<!-- END BLOCK : message -->
<!-- END BLOCK : error -->
<form method="post" action="myscript.php">
Email:
<input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
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();
templatePower (ASSIGNGLOBAL)
assignGlobal ( string variablename, mixed value )
assignGlobal ( array( variablename => value ) )
img.tpl
myscript.php
<html>
<head>
<title>AssignGlobal Example</title>
</head>
<body>
<img src="{imagedir}/logo.gif">
<?php include_once( "./class.TemplatePower.inc.php" );
$tpl = new TemplatePower( "./img.tpl" );
$tpl->prepare();
<!-- START BLOCK : image -->
<img src="{imagedir}/img_{id}.gif">
<!-- END BLOCK : image -->
</body>
</html>
$tpl->assignGlobal( "imagedir", "images");
for ( $i=1; $i<=10; $i++ ) {
$tpl->newBlock( "image" );
$tpl->assign( "id", $i );
}
$tpl->printToScreen();
?>
templatePower IMAGES
templatePower (ASSIGN)
Bestand
Index.tpl
<html>
<head>
<title>index</title>
</head>
<?php
include("class.TemplatePowe
r.inc.php");
$tpl = new
TemplatePower("index.tpl");
$tpl->prepare();
<body>
<h1>Welkom
{persoon}!</h1>
</body>
</html>
$tpl->assign("persoon",
"XenoX");
$tpl->printToScreen();
?>
templatePower (ASSIGN(ARRAY))
<html>
<head>
<title>{titel}</title>
</head>
<body>
<h1>Welkom
{persoon}!</h1>
</body>
</html>
<?php
include("class.TemplatePower.inc.php");
$tpl = new TemplatePower("index.tpl");
$tpl->prepare();
$tpl->assign(array(
"titel" => "TemplatePower",
“persoon" => "XenoX“ )
);
$tpl->printToScreen();
?>
templatePower (ASSIGNINCLUDE)
include.tpl <html>
<head>
<title>AssignInclude Example</title>
</head>
<body>
<!-- INCLUDE BLOCK : header -->
<!-- INCLUDESCRIPT BLOCK : content
-->
</body>
</html>
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();
?>
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
<html>
<head>
<title>AssignInclude Example</title>
</head>
<body>
<!-- START BLOCK : number -->
{number}
<!-- END BLOCK : number -->
{total}
</body>
</html>
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
<html>
<head>
<title>NewBlock</title>
</head>
<body>
<table>
<tr><td>Names</td></tr>
<!-- START BLOCK : name_row -->
<tr> <td>{name}</td> </tr>
<!-- END BLOCK : name_row -->
</table> <br> {total_names}
</body>
</html>
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)
<html>
<head>
<title>{titel}</title>
</head>
<body>
<h1>Welkom {persoon}!</h1>
<!-- START BLOCK : rij -->
{id} : {naam}
<!-- END BLOCK : rij -->
</body>
</html>
templatePower (BLOCK)
<?php
include("class.TemplatePower.inc.php");
$tpl = new TemplatePower("index.tpl");
$tpl->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“ ));
templatePower (BLOCK-NESTED)
<?php
include("class.TemplatePower.inc.php");
$tpl = new TemplatePower("index.tpl");
$tpl->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)
<?php
include("class.TemplatePower.inc.php");
$tpl = new TemplatePower("index.tpl");
$tpl->prepare();
$tpl->assign(array("titel" => "TemplatePower“, "persoon"
=> "XenoX“ ));
$template = $tpl->getOutputContent();
?>
templatePower (FORMS)
form.tpl
<html> <head> <title></title> </head> <body>
<!-- START BLOCK : error --> The following errors occurred.<br>
<!-- START BLOCK : message --> - {message}<br>
<!-- END BLOCK : message -->
<!-- END BLOCK : error -->
<form method="post" action="myscript.php">
Email:
<input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
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