2011-08-03 31 views
5

我一直在嘗試小枝,它適用於我的小網站。分頁與小枝

這是使用教程:

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()); 
} 
?> 

什麼會,我需要做的就是內嫩枝分頁的結果? 否則我的網站工作得很好!

感謝,JC

回答

2

由於嫩枝僅僅是一個模板引擎,沒有任何包括(至少在覈心)來添加分頁。你必須自己分割內容並分頁(例如使用JavaScript)。請記住,在您當前的實施中,完整內容將被插入到模板中,您只能隱藏/顯示它的某些部分。

但是,首選的方法是將分頁也包含在您的模型(您進行查詢的部分)中,以僅加載這些記錄,這些記錄目前向用戶顯示。這顯然超出了模板引擎的範圍。