Template System ersetzt Funktionen an der falschen Stelle

kAmBeR

Mitglied
Hallo,

PHP:
		function rplc($var_array, $template)
		{
			$this->temp_content = file($template); 
			
			foreach($var_array as $key => $value)
		   	{
				//$regex[var_name] : {VARIABLE}
				$regex['var_name'] = "/{[[:space:]]*?(".strtoupper($key)."){1,}?[[:space:]]*?}/si";
				$this->temp_content = preg_replace($regex['var_name'], $value, $this->temp_content);
				echo implode("", $this->temp_content);
		   	}
		}

Ich benutze dies als das Template System, jedoch werden Funktionen nicht an der richtigen Stelle ersetzt. Das liegt darin, dass sie sofort ausgeführt werden. Wie könnte ich das ändern? Sie sollen dort aufgerufen werden, wo sie benötigt werden.

z.B.
HTML:
<body>
{party}
</body>

PHP:
PHP:
	$tpl = new k_tmp();

	$arg = array("party" => topFive("party")); // wird direkt aufgerufen. Andere Möglichkeit?
	$tpl->rplc($arg, "./template/index2.html");

danke im vorraus
mfg
 
übergib einfach den funktionsnamen in deinem array

also zum beispiel rufst du dann so auf:
PHP:
$tpl = new k_tmp(); 

    $arg = array("party" => array("topFive", "party")); // wird direkt aufgerufen. Andere Möglichkeit? 
    $tpl->rplc($arg, "./template/index2.html");

dann änderst du deine replace funktion so:

PHP:
function rplc($var_array, $template) 
        { 
            $this->temp_content = file($template); 
             
            foreach($var_array as $key => $value) 
               { 
                //$regex[var_name] : {VARIABLE} 
                $regex['var_name'] = "/{[[:space:]]*?(".strtoupper($key)."){1,}?[[:space:]]*?}/si"; 
                $this->temp_content = preg_replace($regex['var_name'], $value[0]($value[1]), $this->temp_content); 
                echo implode("", $this->temp_content); 
               } 
        }


Cu Paraman
 
Zurück