If statements in php templates using tpl

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

+5
source share
2 answers

What is wrong with using plain old PHP? It is much faster and much easier.

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

If you really insist, here is 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);
}
?>

It's pretty simple, it gets really hairy when you want to work with nested operators.

+6
source

You can parse this syntax using the smarty template engine.

http://www.smarty.net/crash_course

0
source

All Articles