Templates stored in database

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a form where a user can enter different feild, for simplicities sake,  
say a 'To:', 'From' and 'Message' feild. The can select a 'template' from  
the databse, each one has different layouts, images etc, and is sotred as  
HTML code in the mySql. My question is how do I insert the vairables into  
the correct places in the template? I realise if it was hard-coded I could  
use something like:

echo "To:". $to. "<br><br>". $message. "<br><br>From:". $from;

etc, although this is simplified alot and the ones int databse contain  
images, tables, css etc. I don't really want to hardcode them using IF  
statements as I want it to be easy to add new 'templates'.


Re: Templates stored in database

Ben Allen wrote:
Quoted text here. Click to load it

If you have a limited amount of variables, you could look into the
(s)printf() function.
printf('To:%1$s<br><br>%2$s<br><br>From:%3$s', $to, $message, $from);

Else, you could consider a kind of syntax of your own, like
 $to = 'Me';
 $from = 'You';
 $message = 'message in this case';
 $string = 'To:}}<br><br>}}<br><br>From:}}';
 echo preg_replace('/}}/e','$$1',$string);

I don't really like the e-modifier in this case, maybe others have a better
solution for this. Don't come knocking with eval(), that's even worse :P

Rik Wasmus

Site Timeline