Function Help needed.
Date: 03/08/07
(PHP Community) Keywords: php
I use an old function tossed together by a friend to replace {VARIABLES} in that format in a file like "filename.tmpl" so something like "This is a {VARIABLE}" might output as "This is a test". He used two functions to accomplish this - and I'm trying to condense it into one but I'm running into an error.
Here is the old code and method of execution:
function loadtemplate($tmpl) {
$fp = "$_SERVER[DOCUMENT_ROOT]${tmpl}";
$loadtemplate = file_get_contents($fp);
return $loadtemplate;
}
function replace_tags($content,$vars) {
while (list($key,$value) = each($vars)) {
$key = '{'.$key.'}';
$content = str_replace("$key","$value",$content);
}
return $content;
}
echo replace_tags(loadtemplate("test.tmpl"), array(
'VARIABLE' => "Test"
));
And here is my sleepily modified version:
function contentManager($template, $content, $variables) {
$loadFiles = "$_SERVER[DOCUMENT_ROOT]${template}";
$loadTemplate = file_get_contents($loadFiles);
return $loadTemplate;
while (list($key, $value) = each($variables)) {
$key = '{' . $key . '}';
$content = str_replace($key, $value, $content);
}
return $content;
}
echo contentManager("test.tmpl", array(
'VARIABLE' => "Test"
));
For this - I get an error of: "Warning: Missing argument 3 for contentManager(), called in //Sites/new_function.php on line 21 and defined in /Sites/new_function.php on line 5"
I'm pretty sure this is a really dumb error that I'm missing from sheer eye fatigue - but if someone could help me out I'd be grateful.
Source: http://community.livejournal.com/php/549371.html