TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Template class - help!! (http://www.talkphp.com/general/2877-template-class-help.html)

Sam Granger 06-01-2008 11:29 PM

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?:-)

delayedinsanity 06-02-2008 12:53 AM

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

Kalle 06-02-2008 01:00 AM

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

Sam Granger 07-28-2008 08:04 AM

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 %>

Ross 07-28-2008 11:13 AM

Quote:

Originally Posted by Sam Granger (Post 17424)
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);



Sam Granger 07-28-2008 07:42 PM

Quote:

Originally Posted by redSHIFT (Post 17427)
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? *!*


All times are GMT. The time now is 12:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0