Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination with Twig

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

like image 250
XFS Avatar asked Aug 03 '11 10:08

XFS


2 Answers

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.

like image 80
apfelbox Avatar answered Nov 11 '22 08:11

apfelbox


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

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

like image 5
Capitaine Avatar answered Nov 11 '22 08:11

Capitaine