Help - Search - Members - Calendar
Full Version: How to use PHP as a Template Engine
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Koncept
Well this is my first tutorial ever, i thought i might share how to use php by itself as a template engine.
index.php
CODE
<?php
$title = 'P2L - Koncept';
$template = 'P2';
$content = 'Hello World!';
$admin = false;
include("templates/$template/index.php");
?>

templates/P2/index.php
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?= $title; ?></title>
</head>
<body>
Well here is my content<br />
<?= $content; ?><br />
<?php while($i < 11){ ?>
Test <?= $i; ?><br />
<?php $i++; } ?><br />
<?= if($admin) ? 'I am admin' : 'I am not an admin'; ?>
</body>
</html>

lets explain the code above
CODE
<?= $title; ?>

is the same as
CODE
<?php echo $title; ?>

CODE
<?= if($admin) ? 'I am admin' : 'I am not an admin'; ?>

Is a if statement, if $admin is true it will echo "I am admin" else "I am not an admin"
CODE
<?php while($i < 11){ ?>
Test <?= $i; ?><br />
<?php $i++; } ?>

Is just a while loop that loops trough the html it surrounds

Sorry for lack of explanation, i hope i left enough examples for you guys.
Better tutorials to come when im not sick. victory.gif
Donna
QUOTE
Sorry for lack of explanation, i hope i left enough examples for you guys.
Better tutorials to come when im not sick.


Maybe when your not sick you should have a read of this and make edits above:

http://www.danrichard.com/2006/04/08/writi...torial-portals/

As a bit of code with no explanation whatsoever is not a tutorial.
Matthew.
This isnt really a template engine at all. (sorry lol)

I would suggest this for anyone looking for a good beginner method.
Koncept
this can be implemented as a simple engine ony thing that it wont compile templates like smarty or filearts but you can make a simple compiler class =p
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.