用树枝分页

XFS*_*XFS 5 php pagination templating 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>
Run Code Online (Sandbox Code Playgroud)

这是它的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());
}
?>
Run Code Online (Sandbox Code Playgroud)

我需要做些什么才能在Twig中对结果进行分页?否则我的网站运作得非常好!

谢谢,JC

apf*_*box 5

由于 Twig 只是一个模板引擎,因此没有包含任何内容(至少在核心中)来添加分页。您必须自己拆分内容并对其进行分页(例如使用 JavaScript)。请记住,在您当前的实现中,完整的内容被插入到模板中,您只会隐藏/显示其中的某些部分。

然而,首选的方法是将分页也包含在您的模型中(您执行查询的部分)以仅加载当前显示给用户的这些记录。这显然超出了模板引擎的范围。


Cap*_*ine 5

互联网上已有一些例子.你可以参考

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