IBM developerWorks
 
           Development Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsCommunityDevelopment Tutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old November 19th, 2002, 08:38 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Article Discussion: Writing Your Own Template and Caching Class In PHP

If you have any questions or comments about this article then please post them here.

This forum post relates to this article

Reply With Quote
  #2  
Old November 20th, 2002, 03:13 AM
Lindset Lindset is offline
weirdomoderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Alta, Norway
Posts: 370 Lindset User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to Lindset Send a message via AIM to Lindset
Here's the whole thing.. indented

PHP Code:
<?php 

class Template 

    var 
$_variables = array(); 
    var 
$_caching false
    var 
$_cacheDir 'cache'
    var 
$_cacheLifetime 120

    function 
addVar($name$value
    { 
        
$variables = &$this->_variables
        
$variables[$name] = $value
    } 

    function 
display($file$id false
    { 
        echo 
$this->fetch($file$id); 
    } 

    function 
fetch($file$id false
    { 
        if (
$this->_caching == true && $this->isCached($file$id)) { 
            
$output $this->_getCache($file$id); 
        } else { 
            
$output $this->_getOutput($file); 

            if (
$this->_caching == true) { 
                
$this->_addCache($output$file$id); 
            } 
        } 

        return isset(
$output) ? $output false
    } 

    function 
_getOutput($file
    { 
        
extract($this->_variables); 
        
$file realpath($file); 

        if (
file_exists($file)) { 
            
ob_start(); 
            include(
$file); 
            
$output ob_get_contents(); 
            
ob_end_clean(); 
        } else { 
            
trigger_error("Failed opening the template file '$file'. The file doesn't exist."E_USER_ERROR); 
        } 

        return !empty(
$output) ? $output false
    } 

    function 
setCacheDir($dir
    { 
        
$dir realpath($dir); 

        if (
is_dir($dir) && is_writable($dir)) { 
            
$this->_cacheDir $dir
        } else { 
            
trigger_error("The cache directory '$dir' either doesn't exist, or it cannot be written to by PHP"E_USER_WARNING); 
            
$this->_cacheDir ''
            
$this->_caching false
        } 
    } 

    function 
setCacheLifetime($seconds
    { 
        if (
is_numeric($seconds)) { 
            
$this->_cacheLifetime $seconds
        } 
    } 

    function 
setCaching($state
    { 
        if (
is_bool($state)) { 
            
$this->_caching $state
        } 
    } 

    function 
isCached($file$id false
    { 
        
$id $id md5($id basename($file)) : md5(basename($file)); 
        
$filename $this->_cacheDir '/' $id '/' basename($file); 

        if (
is_file($filename)) { 
            
clearstatcache(); 

            if (
filemtime($filename) > (time() - $this->_cacheLifetime)) { 
                
$isCached true
            } 
        } 

        return isset(
$isCached) ? true false
    } 

    function 
_addCache($content$file$id false
    { 
        
$id $id md5($id basename($file)) : md5(basename($file)); 
        
$filename $this->_cacheDir '/' $id '/' basename($file); 
        
$directory $this->_cacheDir '/' $id

        @
mkdir($directory0775); 

        if (
$fp fopen($filename'wb')) { 
            
fwrite($fp$content); 
            
fclose($fp); 
        } 
    } 

    function 
_getCache($file$id false
    { 
        
$id $id md5($id basename($file)) : md5(basename($file)); 
        
$filename $this->_cacheDir '/' $id '/' basename($file); 

        if (
$fp fopen($filename'rb')) { 
            
$content fread($fpfilesize($filename)); 
            
fclose($fp); 
        } 

        return isset(
$content) ? $content false
    }
 


?>
__________________
Best Regards,
Håvard Lindset

Last edited by Lindset : November 20th, 2002 at 03:30 AM.

Reply With Quote
  #3  
Old September 11th, 2003, 02:23 PM
mattyc mattyc is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 mattyc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No PHP output?

I found you article today and starting playing around with it. I thought it was a great article by the way. But I have a problem. I'll try and keep this short, cause I don't even know if you will reply.

I'm trying to make a content wrapper/template for my company's website. The layout is setup so there is a place in the middle of the page where the content goes and menu bars, pics etc surround the content. At first I just used a query string var to pass the content location(ie /index.html) and used readfile(). This works fine on regular html docs but not php docs, the php code isn't compiled. So then I moved on to trying iframes, which does compile the php, but I hate having a scroll bar within a scrollbar.

So I came to your tutorial and tried it out. The html pages work fine but the php code doesn't get compiled. Do you know a way to compile the php before it is outputted to the screen?

Basically I have it setup so I use an ID to access the db and get the body(html/php code) then I follow your tutorial to the letter. The pure html code is fine, but the php code stays as php code(<?php phpinfo() ?> for example).

Any ideas?

Thanks

Reply With Quote
  #4  
Old October 3rd, 2003, 10:30 AM
Curtis Curtis is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 1 Curtis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to use Object with multiple blocks

Hi,

First, I think the article was great.

How might I use the Template object to display more then one block of repeated database data?

I.e., Show a listing of users and listing of groups on the same page.

Template Power has this feature however I don't want to use it for obvious reason discussed in your article. Plus I really like the cached feature.

Thanks,

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsCommunityDevelopment Tutorials > Article Discussion: Writing Your Own Template and Caching Class In PHP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway