Jump to content

Looping with template class


mattwright

Recommended Posts

How can i loop it so i can make more links?

 

(tried to remove excess code to make it easier to understand/read)

 

Ok my code looks like this

 

 <?php

include "class_template.php";

$global = new template;

$vars = array("linkname" => "Home",
"link"      => "www.home.com");

$page = $global->make_template("global");

echo $global->insert_variables($page, $vars);

?> 

 

and my global template file looks like

 

Menu


<a href="{link}">{linkname}</a>

 

and my class_template looks like this

 

 <?php

class template
{

     var $template_dir     = 'templates/';
     var $file_ext         = '.tpl';
     var $buffer;
    
     function make_template($file)
     {
         $this->buffer = file_get_contents( $this -> template_dir . $file . $this -> file_ext );
        
         return $this->buffer;
     }
    
     function insert_variables($input, $vars)
     {    
         $search = preg_match_all('/{.*?}/', $input, $matches);
                    
         for($i = 0; $i < $search; $i++)
         {
             $matches[0][$i] = str_replace(array('{', '}'), null, $matches[0][$i]);
         }    
         foreach($matches[0] as $value)
         {
             $input = str_replace('{' . $value . '}', $vars[$value], $input);
         }
        
         return $input;
     }

}

?> 

Link to comment
https://forums.phpfreaks.com/topic/56126-looping-with-template-class/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.