Making an efficient template engine
<p>I am trying to make an efficient template engine and I'm struggling to figure out a way to make find a solution. Pretty much everything I've ever seen or come across resembles he following:</p>

<p>You have a template file such as</p>

<pre><code>&lt;html&gt;
&lt;body&gt;
{text}
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>And the next step would be to replace the value in a manner similar to:</p>

<pre><code>$data = str_replace('{text}', 'Hello', $template);
</code></pre>

<p>Which essentially does the job right but what I don't like to do is use functions such as str_replace or sprintf() extensively because they are very slow. And I also want to escape from solutions such as building html output within my functions:</p>

<pre><code>function myfunction(){
global $array_of_data;
foreach ($array_of_data['menu'] as $v)
$menu.='&lt;div class="menu"&gt;&lt;a href="#"&gt;'.$v.'&lt;/a&gt;&lt;/div&gt;';
return $menu;
}
</code></pre>

<p>It will be much much faster but making changes which eventually will happen will end up being a nightmare. Can anyone suggest a more efficient way to balance between the two? Thanks!</p>
