我一直在尝试Twig,它在我的小网站上运行得很好。
这是使用的教程:
http://devzone.zend.com/article/13633
但是,我已经在网上找过了,找不到任何可以进行分页的东西。
这是我的代码:
<html>
<head>
<style type="text/css">
table {
border-collapse: collapse;
}
tr.heading {
font-weight: bolder;
}
td {
border: 0.5px solid black;
padding: 0 0.5em;
}
</style>
</head>
<body>
<h2>Automobiles</h2>
<table>
<tr class="heading">
<td>Vehicle</td>
<td>Model</td>
<td>Price</td>
</tr>
{% for d in data %}
<tr>
<td>{{ d.manufacturer|escape }}</td>
<td>{{ d.model|escape }}</td>
<td>{{ d.price|raw }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>这是它的PHP代码:
<?php
// include and register Twig auto-loader
include 'Twig/Autoloader.php';
Twig_Autoloader::register();
// attempt a connection
try {
$dbh = new PDO('mysql:dbname=world;host=localhost', 'root', 'mypass');
} catch (PDOException $e) {
echo "Error: Could not connect. " . $e->getMessage();
}
// set error mode
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// attempt some queries
try {
// execute SELECT query
// store each row as an object
$sql = "SELECT manufacturer, model, price FROM automobiles";
$sth = $dbh->query($sql);
while ($row = $sth->fetchObject()) {
$data[] = $row;
}
// close connection, clean up
unset($dbh);
// define template directory location
$loader = new Twig_Loader_Filesystem('templates');
// initialize Twig environment
$twig = new Twig_Environment($loader);
// load template
$template = $twig->loadTemplate('automobiles.tpl');
// set template variables
// render template
echo $template->render(array (
'data' => $data
));
} catch (Exception $e) {
die ('ERROR: ' . $e->getMessage());
}
?>我需要做什么才能在Twig中对结果进行分页?除此之外,我的网站运行得很好!
谢谢,JC
发布于 2012-03-15 19:34:37
由于Twig只是一个模板引擎,所以没有包含任何内容(至少在核心中)来添加分页。您必须自己拆分内容并对其进行分页(例如,使用JavaScript)。请记住,在您当前的实现中,完整的内容被插入到模板中,您只需隐藏/显示它的某些部分。
但是,更好的方法是在模型(执行查询的部分)中也包含分页,以便仅加载当前显示给用户的这些记录。这显然超出了模板引擎的范围。
发布于 2013-02-19 18:33:10
互联网上已经有了这样的例子。你可以参考
https://stackoverflow.com/questions/6925297
复制相似问题