用Twig分页
我一直在尝试 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
I've been trying Twig, and it works well for my small site.
This was the tutorial used:
http://devzone.zend.com/article/13633
However, I've had a look online and cannot find anything to do pagination.
This is my code:
<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>
and this is the PHP coding for it:
<?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());
}
?>
What would I need to do to get the results paginated within Twig?
Otherwise my site works perfectly well!
thanks, JC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 Twig 只是一个模板引擎,因此没有包含任何内容(至少在核心中)来添加分页。您必须自己拆分内容并对其进行分页(例如使用 JavaScript)。请记住,在您当前的实现中,完整内容已插入到模板中,您只需隐藏/显示其中的某些部分。
然而,首选方法是将分页也包含在模型中(执行查询的部分)以仅加载当前向用户显示的这些记录。这显然超出了模板引擎的范围。
Since Twig is just a template engine, there is nothing included (at least in the core) to add pagination. You have to split the content by yourself and paginate it (for example using JavaScript). Keep in mind that with your current implementation the complete content is inserted into the template and you would only hide/show some parts of it.
The preferred way however, would be to include the paging also in your model (the part where you do your query) to load only these records, which are currently shown to the user. This is obviously out of the scope of a template engine.
互联网上已经有例子了。您可以参考
https://gist.github.com/SimonSimCity/4594748
There are already examples in the internet. You may refer to
https://gist.github.com/SimonSimCity/4594748