You are here: Re: Templates stored in database « PHP Programming Language « IT news, forums, messages
Re: Templates stored in database

Posted by Rik on 05/28/06 19:57

Ben Allen wrote:
> Hi,
> 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'.

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
'{{{var_name}}}'.
$to = 'Me';
$from = 'You';
$message = 'message in this case';
$string = 'To:{{{to}}}<br><br>{{{message}}}<br><br>From:{{{from}}}';
echo preg_replace('/{{{([a-zA-Z0-9_\-]*)}}}/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

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация