TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 08-12-2009, 11:37 AM   #1 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Bricks PHP Template

Greetings! I am currently making a few projects, but I am very worried about how I should do my templating system so I do not at some point regret how I did it, and want to recode it!

Basically, I just want to insert informations into my sites easily but also, it shouldn't become a mess with shit loads of php calls inside the HTML. I could pretty easily do something like this:

PHP Code:
<?php $articles $object->fetchArticles(); ?>
<?php 
foreach ($articles as $article) : ?>
    <h1><?php echo $article['title']; ?></h1>
    <p><?php echo $article['content']; ?></p>
<?php endforeach; ?>
That'd basically just require the fetchArticles method to return an array, containing an array for each article:

Example:
PHP Code:
public function fetchArticles() {
   
$query connectToDbAndRetrieveInformation();

   while (
$row mysql_fetch_array($query)) {
      
$data[] = array(
         
'title' => $row['title'],
         
'content' => $row['content']
         
// And for more fancy stuff something like this:
         
'time' => $this->timeDifference($row['time']),
         
'author' => $this->authorName($row['author_id'])
      );
   }
   
   return 
$data;

However, this probably is a pretty bad way to do it (I think?), I don't want to use something big as Smarty and have a whole different weird markup (the foreach loops in smarty are just weird) for the foreach loops. (And it's slooow.. And I won't need half the features) I have made some pretty simple stuff involving replacing {something} with a variable set. However, I can't use PHP in the template file (like if I needed to check something before printing something out, like if the user is administrator). Here is what I have currently:

PHP Code:
<?php
class Template {
    private 
$path 'template/';
    private 
$values = array();

    private function 
__set($name$value) {
        
$this->values[$name] = $value;
    }

    public function 
display($file) {
        
$file $this->path $file;
    
        
$file_content file_get_contents($file);
            
        foreach (
$this->values as $key => $value) {
            
$file_content str_replace('{' $key'}'$value$file_content);
        }    

        echo 
$file_content;
      }
}
?>
Maybe this could be expanded to be useful?

I hope I can get some help on doing this part properly. :)
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get PHP 5. to work? Newbie windows PHP guy DotNetTim Absolute Beginners 11 02-01-2013 11:02 AM
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
Pure PHP template class. abiko Advanced PHP Programming 1 04-02-2008 05:45 PM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM


All times are GMT. The time now is 08:51 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design