PHP Template system - PHP4-5

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Hey all,

Well I know it took me agggess to find a nice way to create a working template system in php, so loonngg ago I did some searching and editing and the whole works, and here's a simple template system... that works...

CREDIT: Most of code found at codewalkers.com - thanks codewalkers :D

functions.php:
PHP:
<?php

class Page
{
    function Page($template)
    {
        if (file_exists($template))
        {
            $this->page = implode('', file($template));
        }
        else
        {
            die("Template file: $template cannot be found.");
        }
    }

    function parse($file)
    {
        ob_start();
        include($file);
        $buffer = ob_get_contents();
        ob_end_clean();

        return $buffer;
    }

    function replace_tags($tags = array())
    {
        if (sizeof($tags) > 0)
        {
            foreach ($tags AS $tag => $data)
            {
                $data = (file_exists($data)) ? $this->parse($data) : $data;
                $this->page = str_replace('{' . $tag . '}', $data, $this->page);
            }
        }
    }

    function output()
    {
        echo $this->page;
    }
}

?>

index.php:
PHP:
<?php

require_once('template.class.php');

$tpl = new Page('design.html');
$tpl->replace_tags(array(
    'title' => 'This is the title of my html page',
    'content' => 'Here\'s some content which I am putting inside the page, yaaay.'
));
$tpl->output();

?>

design.html
Code:
<html>
<head>
<title>{title}</title>
</head>

<body>

<p>{content}</p>

</body>
</html>

And there you have it, a simple template system, that's quick, and WORKS! :D

Tested on PHP4 and PHP5 servers.

PS - 2ndV added a few (or should I say, took away a few ;) lines of code also - Thanks).

All the best,
Rhett.
 
Last edited:
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I've edited it just a tad, if you don't mind me posting it here Billy :)
PHP:
<?php

class Page
{
    function Page($template)
    {
        if (file_exists($template))
        {
            $this->page = implode('', file($template));
        }
        else
        {
            die("Template file: $template cannot be found.");
        }
    }

    function parse($file)
    {
        ob_start();
        include($file);
        $buffer = ob_get_contents();
        ob_end_clean();

        return $buffer;
    }

    function replace_tags($tags = array())
    {
        if (sizeof($tags) > 0)
        {
            foreach ($tags AS $tag => $data)
            {
                $data = (file_exists($data)) ? $this->parse($data) : $data;
                $this->page = str_replace('{' . $tag . '}', $data, $this->page);
            }
        }
    }

    function output()
    {
        echo $this->page;
    }
}

?>

Would use it nearly the same way as the original, however, no need to place {} around your tags in the replace_tags array. Also, no need to echo output() :)

Taken from Billy's original post, replacing w/my changes:
PHP:
<?php

require_once('template.class.php');

$tpl = new Page('design.html');
$tpl->replace_tags(array(
    'title' => 'This is the title of my html page',
    'content' => 'Here\'s some content which I am putting inside the page, yaaay.'
));
$tpl->output();

?>

design.html
Code:
<html>
<head>
<title>{title}</title>
</head>

<body>

<p>{content}</p>

</body>
</html>
 
0
•••
umm ok i need to make a news system udner one of my pages and im using this template system...the sites is kinda small since its for a school project

anyway so how would i go about adding the news system in the content area?

would i hve 2 make a completely new file with the news system and do:

$page = new Page("pages/news.php");
 
0
•••
Hey 2nV, thanks for the update :D - Original post updated with some credit for your help.

wackyjoe, im not quite sure what your getting at. This system would only be used for websites looking to maintin multiple templates, if your only going to use one, then there really is no need at all.

Rhett.
 
0
•••
Is it possible to output alot of content and some PHP within the array? Would I have to do something like:

PHP:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
 
0
•••
1
•••
=\
Theives!
I use PEAR :: HTML_TEMPLATE_IT
 
0
•••
blacksnday said:
Hrmm....
not to be any party pooper...
but this code looks remarkably similar to CodeWalkers Template Tutorial located at:
http://codewalkers.com/tutorials/58/8.html
Yep it is,

As stated in my first post I gathered it from other sites and added/deleted/edited small parts of it.

I couldn't give credit to the exact site, as I had no idea where I got it from.

Added to first post, thanks for finding, and rep left.
 
0
•••
It still looks good ;)
When I get some time I might use it with my page, smarty might be too massive for me ...
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy — Zero Commission
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back