I am wondering the best way to store html templates in a database where I am able to retrieve them and insert variables.
Obviously I could store them as php and then ‘EVAL’ the record…. but I heard it was evil.
is there a better way? 🙂
edit:
Sorry I wasn’t clear… I tried to simplify it by saying html templates… what I really meant was small embed-able html elements (think youtube)… rather than an entire site.
I would also advice against storing the HTML inside the database. It’s much more convenient to store them as templates on the file-system and include them or parse them when needed. I also recommend using something like Smarty. A very handy tool, that is.
However, if you would rather do it manually, here is what I would do.
First, I would store the template in a file on the file-system. If you would rather use a database, that can be done to. Just know that a database will, usually, cause more overhead when used for stuff like this.
For example, in the case of a YouTube video:
Then I would simply
str_replacethe PHP variables in there, much like PHP itself does with strings.And that would be it.
You could of course use
<?php $width; ?>-like placeholders and justincludethe template, but that leaves you at risk for injection attacks. This is the safer route.