开发者

if statements in php templates using tpl

开发者 https://www.devze.com 2023-02-11 05:32 出处:网络
How can i parse {if game > 4}{someco开发者_如何学Cntent}{/if} from a template using PHP.What\'s wrong with using plain old PHP? It\'s much faster and a whole lot simpler.

How can i parse {if game > 4}{someco开发者_如何学Cntent}{/if} from a template using PHP.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消