Template system

Status
Not open for further replies.

GarettM

Posting Freak
Aug 5, 2010
833
136
Ok Here is what i thought would work

PHP:
<?php
################################################
# Template System - Created By GarettMcCarty
################################################
 
class template {
    public $value = array(); // holds values
   
    public function add($name,$value)
    {
        $this->value[$name] = $value; // Stores array
    }
    public function filter($values)
    {
        foreach($this->value as $replace => $data) // gets Arrays Of $value
        {
            $values = str_ireplace("%".$replace."%", $data, $this->value);
        }
      return $values;
    }
    public function display()
    {
        return ($this->filter($this->value)); // returns filtered Values
    }
}
?>

But it wont work for nothing :/ i think its the str_ireplace i think the ending value isnt correct :( but any help would be a great help
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
There are a few template systems knocking about in the Coders Paradise section, try looking at them and comparing them to yours to see what is incorrect.
 

Xenous

o shi
Nov 15, 2011
383
101
Yer str_ireplace was off a little, I'm still not sure where you're getting the file to be parsed from in that class but that fixed the code (I think)
PHP:
<?php
################################################
# Template System - Created By GarettMcCarty
################################################
 
class template {
    public $value = array(); // holds values
 
    public function add($name,$value)
    {
        $this->value[$name] = $value; // Stores array
    }
    public function filter($values)
    {
        foreach($this->value as $replace => $data) // gets Arrays Of $value
        {
            $values = str_ireplace("%".$replace."%", $data, $values);
        }
      return $values;
    }
    public function display()
    {
        print ($this->filter($this->value)); // returns filtered Values
    }
}
?>
 

GarettM

Posting Freak
Aug 5, 2010
833
136
i look at others SupaTPL and he used private tpl variable to get the page imm try something like that and see if that works
 

Xenous

o shi
Nov 15, 2011
383
101
Should work a charm although I do suggest templating engines such as or .
PHP:
<?php
 
    class AvalozTemplate
    {
        private $params = Array();
        private $filter;
        private $dir = 'system/template';
        
        public function setDir($dir)
        {
            $this->dir = $dir;
        }
        
        public function set($key, $value)
        {
            $this->params['{' . $key . '}'] = $value;
        }
        
        public function arraySet($key, $value)
        {
            @$this->params['{{' . $key . '}}'] .= $value;
        }
        
        private function filter($file)
        {
            foreach($this->params as $key => $value)
            {
                $file = str_ireplace($key, $value, $file);
            }
            return $file;
        }
        
        public function add($file)
        {
            if(file_exists($this->dir . '/' . $file))
            {
                //Allows php code execution inside template files.
                ob_start();
                include($this->dir . '/' . $file);
                $this->filter .= ob_get_contents();
                ob_end_clean();
            }
        }
        
        public function output()
        {
            echo $this->filter($this->filter);
        }
    }
    
    //Basic usage
    $var = new Template();
    $var->setDir('templates');
    $var->set('Hi', 'Hello there fellow member!');
    $var->add('file.html');
    $var->output();
    
    /* Testing html file.
     * 
     * <html>
     * <head>
     * <title>
     * {hi}
     * </title>
     * </head>
     * <body>
     * {hi}
     * </body>
     * </html>
     */
    
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top