Simple if/else parser for template engine
<p>Hey. I need a little help enhancing my IF/ELSE parser (for a simple template engine).
Right now I can use:</p>

<pre><code>{if:something=value}Some info{/if}
</code></pre>

<p>or</p>

<pre><code>{if:something=value}Some info{if:somethingelse=value}even more info{/if}{/if}
</code></pre>

<p>or</p>

<pre><code>{if:something=value}Some info{%else%}Some other info{/if}
</code></pre>

<p>The problems start when I have to write something a bit more complicated, for example:</p>

<pre><code>{if:something=value}
 {if:somethingelse=value}Some info{%else%}Some other info{/if}
{%else%}
 Totally different info
{/if}
</code></pre>

<p>The IF-s get parsed recursively as they should, but since I'm using a simple "explode" to find the "else" value, it just returns the first %else% (DoItDifferently) if something=false.</p>

<p>My code atm: </p>

<pre><code>function parseIfs($input) {
    $regex = '#\{if\:?"?(.*?)"?\}((?:[^{]|\{(?!/?if\:?"?(.*?)"?\})|(?R))+)\{/if\}#i'; 
    if(is_array($input)) {
        // IF - value (defaults to "true" if not defined)
        $block = explode('=',$input[1]); if(empty($block[1])) $block[1] = true;
        // Explode if:value 
        $condition = explode(':',str_replace('!','',$block[0])); 
        // Get the problematic "else" value
        $outcome = explode('{%else%}',$input[2]);
        global ${$condition[0]};
        // Value to check against (can handle arrays - something:something1:something2 =&gt; $something[something1][something2])
        $replacement = ${$condition[0]}; for($i = 1; $i &lt; count($condition); $i++) $replacement = $replacement[$condition[$i]];
        if(!strpos($block[0],'!') ? $replacement == $block[1] : $replacement != $block[1])
            $input = $outcome[0];
        else 
            $input = !empty($outcome[1]) ? $outcome[1] : '';
    }
    return trim(preg_replace_callback($regex, 'parseIfs', $input));
}
</code></pre>
