title: Simple pagination with twig
body: <p>I'm trying to create a simple pagination with a twig view.
  I'm not using Symfony. </p>
  
  <p>Here is my method from my manager :</p>
  
  <pre><code>  public function getAllPosts()
    {
        if(isset($_GET['p']) &amp;&amp; (!isset($_GET['page']))){
  
            $currentPage =  1;
        }
        else {
            $currentPage = $_GET['page'];
        }
      $q= $this-&gt;_db-&gt;query('SELECT COUNT(id) AS numberposts FROM posts');
      $data = $q-&gt;fetch(PDO::FETCH_ASSOC);
  
      $number_posts= $data['numberposts'];
      $perPage = 1;
      $numberPages = ceil($number_posts/$perPage);
  
      $q = $this-&gt;_db-&gt;query("SELECT * FROM posts ORDER BY date DESC LIMIT ".(($currentPage-1)*$perPage).",$perPage");
  
      while($data = $q-&gt;fetch(PDO::FETCH_ASSOC))
      {
        $datas[] = new Post($data);
      }
  
  
      return $datas;
  
    }
  </code></pre>
  
  <p>I want to create a loop in my view, this is what I'm doing </p>
  
  <pre><code>            {% for posts in allPosts %}
                 {% for i in 1..numberPages %}
                      &lt;a href="index.php?p=blog&amp;page={{ i }}"&gt;{{ i }}&lt;/a&gt;
                   {% endfor %}
              {% endfor %}
  </code></pre>
  
  <p>But it's not working. It seems like I can't access to numberPages and I don't know why. </p>
  
  <p>If anybody can help me ! </p>
  
  <p>Thanks a lot</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My pagination is working now.</p>
  
  <p>I had this in my method like @darkbee : </p>
  
  <pre><code>        return array(
      'records'     =&gt; $datas,
      'numberPages' =&gt; $numberPages,
  );
  </code></pre>
  
  <p>And in my view : </p>
  
  <pre><code>{% for i in 1.. allPosts.numberPages %}
                      &lt;li&gt;&lt;a href="index.php?p=blog&amp;page{{ loop.index }}"&gt;{{ loop.index}}&lt;/a&gt;&lt;/li&gt;
                  {% endfor %}
  </code></pre>
  
  <p>But now I have another issue. I only get the same posts in all the pages. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I forgot the page= on my pages links ... </p>
  
  <pre><code> &lt;li&gt;&lt;a href="index.php?p=blog&amp;page={{ loop.index }}"&gt;{{ loop.index}}&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>It's working now !</p>
  
  <p>Thanks !</p>