lsolesen / php-template

轻量级模板引擎,具有与Savant3和Smarty相同的接口。

1.0.1 2016-03-19 15:29 UTC

This package is auto-updated.

Last update: 2024-08-24 23:58:35 UTC


README

非常简单的PHP 4版本模板引擎。它基于Brian Lozier的超越模板引擎文章。

对于相同想法的更现代的PHP 5实现,请查看phpsavant.com

示例

<?php
$path = './templates/';

tpl = new Template($path);
$tpl->set('title', 'User List');
$body = new Template($path);
$body->set('user_list', fetch_user_list());
$tpl->set('body', $body->fetch('user_list.tpl.php'));
echo $tpl->fetch('index.tpl.php');
?>

并且可以使用以下模板。

<!-- user_list.tpl.php -->
<table>
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Banned</th>
    </tr>
    <? foreach($user_list as $user): ?>
    <tr>
        <td align="center"><?=$user['id'];?></td>
        <td><?=$user['name'];?></td>
        <td><a href="mailto:<?=$user['email'];?>"><?=$user['email'];?></a></td>
        <td align="center"><?=($user['banned'] ? 'X' : '&nbsp;');?></td>
    </tr>
    <? endforeach; ?>
</table>

<!-- index.tpl.php -->

<html>
    <head>
        <title><?=$title;?></title>
    </head>
    <body>
        <h2><?=$title;?></h2>
        <?=$body;?>
    </body>
</html>