I created some templating system to help me output database items in a skinnable way. It works like this:
$tpl = "<html>
<body>
<div><span class="item">Favourite food:</span><span class="content">{item1}</span></div>
<div><span class="item">Hobby:</span><span class="content">{item2}</span></div>
<div><span class="item">Allergies:</span><span class="content">{item3}</span></div>
// up to 500 more lines like this
</body>
</html>";
$output = str_replace(
array('{item1}','{item2}','{item3}') ) ,
array($database['item1'],$database['item2'],$database['item3']) ,
$tpl );
echo $output;
Now I the problem is: Not every database item has content necessarily - so, currently I get output like this
Favourite food: Banana Hobby: {item2} Allergies: Apple
Is there any way to use this kind system and to leave out the lines that don't have a database entry? The output should then be like this:
Favourite food: Banana Allergies: Apple
How could I achieve this?
UPDATE: The Description text may vary, as well, therefore I updated the example code. And I would like to avoid foreach or for-loops because then the templating itself is broken. I want to be able to insert random HTML. Using smarty or another templating system is not feasible at this moment. But I could provide a third array with descriptions.
UPDATE 2: I thought that maybe a preg_replace on $output might do the trick? so this would mean "IF there is a html tag that contains {} brackets somewhere in it on $output THEN erase the whole tag from to (I updated my code accordingly) what do you think? do you know any reg_expression that could do this?