PHP Template system - PHP4-5

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
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
Close
Thanks for the script, I will definately use it. You can almost make an entire CMS system based on that, sweet! :)
 
0
Close
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
Close
thxs for this, will come in usefull :)
 
0
Close
No problem :), hope it helps!
 
0
Close
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
Close
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
Close
^^ 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
Close
0
Close
0
Close
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
Close
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
Close
hi,

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

cheers,

-ed
 
0
Close
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
Close
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
Close
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
Close
Nice work Billy.

- Steve
 
0
Close
Thanks a lot.
I will use it.
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back