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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-01-2008, 11:29 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default Template class - help!!

I have written the following simple class - first of all, if I can improve any parts, please let me know!!

PHP Code:
<?php

/**
 * @author Sam Granger
 * @copyright 2008, *******.com
 */

class Template {

    var 
$template;
    
    function 
__construct($template_file) {

        
$this->template file_get_contents($template_file);

    }
    
    function 
replaceTags($variable$content) {
        
$this->template str_replace('<% ' $variable ' %>'$content$this->template);
    }
    
    function 
displayHTML() {
        echo 
$this->template;
    }

}

?>
Used as following:

PHP Code:
$template = new Template('design.html');
$template->replaceTags('title''Hello!');
$template->replaceTags('message''Under construction');
$template->displayHTML(); 
Whats the best way to implement variables that are outputted from a while or for statement?
Sam Granger is offline  
Reply With Quote
Old 06-02-2008, 12:53 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

PHP Code:
for ($aArray as $key => $value) {
    
$template->replaceTag($key$value);

Seems like too easy an answer though, but I don't know, if you post an example of what you're trying to do perhaps?
-m
delayedinsanity is offline  
Reply With Quote
Old 06-02-2008, 01:00 AM   #3 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

The best way to do that would be to make a replaceTags version that accepts arrays and then iterates though them like:

PHP Code:
public function replaceTags($tags$value NULL)
{
    if(
is_array($tags) && sizeof($tags))
    {
        
$value     array_values($tags);
        
$tags    array_map(Array($this'appendTags'), array_keys($tags));
    }

    
$this->template str_replace($value$value$this->template);
}

private function 
appendTags($variable)
{
    return(
'<% ' $variable ' %>');

After seeing you use PHP5 I would use the __toString() magic method for the output like:
PHP Code:
public function __toString()
{
    return((string) 
$this->template);

And then you can use it like:
PHP Code:
$template = new Template('test.html');
/* variables ... */
echo $template
For the iterator to work then you are able to do like:
PHP Code:
while(/* statement */)
{
    
/* Assuming $data is an array */
    
$template = new Template('output.html');
    
$template->replaceTags($data);

    echo 
$template;


I would also recommed you to use the private/protected/public modifiers =)

Hope this helps
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 07-28-2008, 08:04 AM   #4 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Sorry for the old bump.

Want to output each row from a mysql while statement. Whats the best way to get around this?

Meant something so in the template I can add:

<% beginwhile:results %>
<div>
<h3><% Name %></h3>
<p><% Address %></p>
</div>
<% endwhile:results %>
Sam Granger is offline  
Reply With Quote
Old 07-28-2008, 11:13 AM   #5 (permalink)
The Contributor
 
Ross's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
Ross is on a distinguished road
Default

Quote:
Originally Posted by Sam Granger View Post
Sorry for the old bump.

Want to output each row from a mysql while statement. Whats the best way to get around this?

Meant something so in the template I can add:

<% beginwhile:results %>
<div>
<h3><% Name %></h3>
<p><% Address %></p>
</div>
<% endwhile:results %>
I'd go for something like this:

PHP Code:
public function replace_while($name, Array $data)
{
    if(
strstr('<% beginwhile:' strtolower($name) . ' %>'$this->template) && strstr('<% beginwhile:' strtolower($name) . ' %>'$this->template))
    {
        foreach(
$data as $key => $value)
        {
            
$this->replace_tags($key$value);
        }
        
        
$this->replace_tags('beginwhile:' strtolower($name), '');
        
$this->replace_tags('endwhile:' strtolower($name), '');
    }

Then to call it you'd do something like:

PHP Code:
$results_d $db->query('SELECT * FROM `results` WHERE `x` = 4 ORDER BY `the_moon`');

if(
$results_d->num_rows 1)
{
    
// blah
}
else
{
    
$users = new Template('users');
    
    
$results = array();
    while(
$result $results_d->fetch_assoc())
    {
        foreach(
$result as $key => $value)
        {
            
$results[$key] = $value;
        }
    }
    
    
$users->replace_while('results'$results);

Ross is offline  
Reply With Quote
The Following User Says Thank You to Ross For This Useful Post:
Sam Granger (07-28-2008)
Old 07-28-2008, 07:42 PM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Quote:
Originally Posted by redSHIFT View Post
PHP Code:
$results_d $db->query('SELECT * FROM `results` WHERE `x` = 4 ORDER BY `the_moon`');

if(
$results_d->num_rows 1)
{
    
// blah
}
else
{
    
$users = new Template('users');
    
    
$results = array();
    while(
$result $results_d->fetch_assoc())
    {
        foreach(
$result as $key => $value)
        {
            
$results[$key] = $value;
        }
    }
    
    
$users->replace_while('results'$results);

Ok, I think this is what I'm after but the code above, lets say my query 'messageresult' returns 'title' & 'message' - how do I send both those variables to the template?
Sam Granger is offline  
Reply With Quote
Reply



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


All times are GMT. The time now is 07:40 AM.

 
     

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