Click to See Complete Forum and Search --> : Basic Template Function


atad6
03-14-2005, 10:35 PM
Hi,
I've started working on this and was wondring if i was heading in the right direction. This is a function that takes an array and a template file and replaces all instances of {key} where key is a different key in the array by the data that corresponds to that key. So as an example, $array['value1'] = "Test"; would read through the template file and replace all instances of {value1} with the word Test. I just wanted to know if i was doing this the right way. It's quite a simple function which has me worried about whether or not I did this correctly.


function template($tloc, $tdata)
{
if (!file_exists($tloc))
{
echo ("Template file does not exist");
}
else
{
$tpl = file_get_contents($tloc);
foreach (array_keys($tdata) as $c)
{
$tpl = str_replace('{'.$c.'}', $tdata[$c], $tpl);
}
}
return $tpl;
}


Thanks!

ShawnK
03-14-2005, 11:14 PM
This is a bit odd...not more than 30 minutes ago I posted code very very similar to this and here it is. Is it just me or does it seem like more than a coinsidense.

atad6
03-14-2005, 11:28 PM
Ah, I'm sorry, that makes me feel kinda weird too, I dont't know what to say. It probably looks like I blatantly tried to copy you and now I'm covering it up. I know it's hard to believe, but I didn't even see that earlier, I've just been working on this function by myself with no references (except the php documentation). I went to phpbuilder after I finished it hoping someone might give me some suggestions on my efforts.I know you probably won't believe me, I find it a bit weird myself. So i'm sorry that you think that. I just hope maybe you'd trust me (have them check the log files if you want, i just want to be trusted), I'm just looking for a little help.

ShawnK
03-14-2005, 11:39 PM
Ok, I apoligize then. I just found it kinda wierd :p

You are doing it right, although it may be more helpful in the future if you make your variables a little more descriptive so we can tell exactly what they are.

atad6
03-14-2005, 11:44 PM
Thanks for understanding and for the suggestions. It was just so weird and I didn't really know what to say, so yeah, thanks for trusting me on this and for the suggestions.