Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statements in php templates using tpl

How can i parse {if game > 4}{somecontent}{/if} from a template using PHP.

like image 803
Speedy Wap Avatar asked Feb 16 '11 01:02

Speedy Wap


1 Answers

What's wrong with using plain old PHP? It's much faster and a whole lot simpler.

<?php if ( $game > 4 ): ?>
some content
<?php endif ?>

If you really insist, here's a start (untested):

<?php
preg_match_all('/\{if ([^}]+)\}.+?\{\/if\}/s', $content, $matches)

foreach ( $matches as $match )
{
    $expression = $match[1];

    // Evaluate expression

    $content = preg_replace($match[0], $true ? $match[1] : '', $content);
}
?>

This is pretty simple, it get's really hairy when you want to work with nested statements.

like image 173
Elbert Alias Avatar answered Nov 04 '22 00:11

Elbert Alias