用Twig分页

发布于 2024-11-28 04:28:37 字数 2306 浏览 3 评论 0原文

我一直在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-12-05 04:28:37

由于 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.

甜尕妞 2024-12-05 04:28:37

互联网上已经有例子了。您可以参考

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文