简体   繁体   中英

if statements in php templates using tpl

如何使用PHP从模板解析{if game > 4}{somecontent}{/if}

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.

You can parse that syntax using smarty template engine.

http://www.smarty.net/crash_course

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM