IT.COM

PHP Template system - PHP4-5

Spaceship Spaceship
Watch

BillyConnite

<img src="images/smilies/biggrin.gif" border="0" cVIP Member
Impact
109
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.
Just a note, really no need to use eregi_replace. str_replace will do the job just fine, and is faster than both ereg(i)_replace and preg_replace :)

And I'll include a quote from the php manual
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

Nice none-the-less.
 
Last edited:
0
•••
Thanks 2ndver :D, I'll change it now, every little bit of optimization counts.

Just did a test on the two, and you're right 2ndver :o ;).

This is with preg_replace:
0.000887870788574 seconds
0.000899076461792 seconds
0.000828981399536 seconds
This is with str_replace:
0.000752925872803 seconds
0.000725030899048 seconds
0.000748872756958 seconds
So str_replace is around 0.0009 seconds faster :D - Every little thing counts!
 
Last edited:
0
•••
Thanks for the script, I will definately use it. You can almost make an entire CMS system based on that, sweet! :)
 
0
•••
BillyConnite said:
Thanks 2ndver :D, I'll change it now, every little bit of optimization counts.

Just did a test on the two, and you're right 2ndver :o ;).

This is with preg_replace:

This is with str_replace:

So str_replace is around 0.0009 seconds faster :D - Every little thing counts!
No prob ;)
 
0
•••
thxs for this, will come in usefull :)
 
0
•••
No problem :), hope it helps!
 
0
•••
Sorry to ask. I am not a php expert. But what exactly would this do? What is the difference from making a phpinclude and just include it in pages?

Thanks,
Camron
 
0
•••
StackedTech said:
Sorry to ask. I am not a php expert. But what exactly would this do? What is the difference from making a phpinclude and just include it in pages?

Thanks,
Camron
It parses the page, rather than including it. It also prevents php scripts from being embedding in the template.

Putting {TITLE} on a page is a lot safter than <?=$pagetitle?>
 
0
•••
^^ What Jim said :).

It's good too though so that when your selling a php script, and have a template system that parses the pages, the template can be written in 100% html or css so if the buyer has no php knowledge it is much easier and not confusing for them to create a template for the script than when using includes.

All the best, Rhett.
 
0
•••
0
•••
0
•••
Hi,

This template system is great, but im having trouble using php on the page.
Putting php in the array doesnt work.

e.g.
PHP:
'{CODE}' => 'php code here'

any tips?

thanks,

-ed
 
0
•••
Hey ed,

What you could do is parse the php you want in the array first?

So instead of say:
'{CODE}' => '$variable=1+1*10'

You could do:
$variable=1+1*10;
'{CODE}' => $variable

Ofcourse it'd work with any functoins etc you wanted also... BUT I believe there is another way to do it, forgotten how though, I'll see if I can find it for you.
 
0
•••
hi,

thanks, i never though about doing that, lol i feel stupid now ;)

cheers,

-ed
 
0
•••
I am trying to add images on same way but it wont show it.... DO i need to make some function to display images? t

$images="images/some.jpg"

to array i add
{IMG} => $images

to design.html

<p>{IMG}</p> but i just see error on line $images, and it only show "jpg" in this line where it call for images..

THnx
 
0
•••
crow said:
I am trying to add images on same way but it wont show it.... DO i need to make some function to display images? t

$images="images/some.jpg"

to array i add
{IMG} => $images

to design.html

<p>{IMG}</p> but i just see error on line $images, and it only show "jpg" in this line where it call for images..

THnx

try:

$images = "<img src='images/some.jpg' border='0' alt='' />";
{IMG} => $images

:)

Regards,
Peter
 
0
•••
Hey crow,

If the page is in a folder, then usually the image path will be wrong as compared with that folder... if you know what I mean.

So you'll have to try the full path i.e. http://domain.com/images/image.jpg instead of just images/image.jpg

Let me know how it goes,
Rhett.
 
0
•••
Nice work Billy.

- Steve
 
0
•••
Thanks a lot.
I will use it.
 
0
•••
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
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back