Note that there are some explanatory texts on larger screens.

plurals
  1. POadvanced php templating using str_replace arrays
    primarykey
    data
    text
    <p>I created some templating system to help me output database items in a skinnable way. It works like this:</p> <pre><code>$tpl = "&lt;html&gt; &lt;body&gt; &lt;div&gt;&lt;span class="item"&gt;Favourite food:&lt;/span&gt;&lt;span class="content"&gt;{item1}&lt;/span&gt;&lt;/div&gt; &lt;div&gt;&lt;span class="item"&gt;Hobby:&lt;/span&gt;&lt;span class="content"&gt;{item2}&lt;/span&gt;&lt;/div&gt; &lt;div&gt;&lt;span class="item"&gt;Allergies:&lt;/span&gt;&lt;span class="content"&gt;{item3}&lt;/span&gt;&lt;/div&gt; // up to 500 more lines like this &lt;/body&gt; &lt;/html&gt;"; $output = str_replace( array('{item1}','{item2}','{item3}') ) , array($database['item1'],$database['item2'],$database['item3']) , $tpl ); echo $output; </code></pre> <p>Now I the problem is: Not every database item has content necessarily - so, currently I get output like this</p> <blockquote> <p>Favourite food: Banana Hobby: {item2} Allergies: Apple</p> </blockquote> <p>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:</p> <blockquote> <p>Favourite food: Banana Allergies: Apple</p> </blockquote> <p>How could I achieve this?</p> <p>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. </p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload