与嫩枝分页 [英] Pagination with Twig

查看:96
本文介绍了与嫩枝分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试Twig,它在我的小型网站上效果很好.

I've been trying Twig, and it works well for my small site.

这是所使用的教程:

http://devzone.zend.com/article/13633

但是,我在网上看过一遍,找不到任何可以做分页的东西.

However, I've had a look online and cannot find anything to do pagination.

这是我的代码:

    <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中分页显示结果,我需要怎么做? 否则,我的网站将运行良好!

What would I need to do to get the results paginated within Twig? Otherwise my site works perfectly well!

谢谢,JC

推荐答案

互联网上已经有示例.您可以参考

There are already examples in the internet. You may refer to

https://gist.github.com/SimonSimCity/4594748

这篇关于与嫩枝分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆