Jump to content


Photo
- - - - -

How to use PHP as a Template Engine


  • Please log in to reply
3 replies to this topic

#1 Koncept

Koncept

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 04 May 2006 - 09:37 PM

Well this is my first tutorial ever, i thought i might share how to use php by itself as a template engine.
index.php
<?php
$title = 'P2L - Koncept';
$template = 'P2';
$content = 'Hello World!';
$admin = false;
include("templates/$template/index.php");
?>
templates/P2/index.php
<!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
<?= $title; ?>
is the same as
<?php echo $title; ?>
<?= 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"
<?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. :)

Edited by Koncept, 04 May 2006 - 11:35 PM.


#2 Donna

Donna

    Retired P2L Queen!

  • P2L Staff
  • PipPipPipPip
  • 12,330 posts
  • Gender:Female
  • Location:B.C Canada

Posted 04 May 2006 - 10:26 PM

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.danrichar...torial-portals/

As a bit of code with no explanation whatsoever is not a tutorial.

#3 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 10:08 AM

This isnt really a template engine at all. (sorry lol)

I would suggest this for anyone looking for a good beginner method.

#4 Koncept

Koncept

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 14 May 2006 - 04:30 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users