<% if (_.isNumber(totalRecords)) { %>
  <div class="pagination-beside">
    <span class="item-count">Items found: <%- totalRecords %></span>
  </div>
<% } %>
<ul>
  <%
    if (_.isNumber(totalPages)) {
      var start = Math.max(Math.min(currentPage - 2, totalPages - 4), 1);
      var end = Math.min(Math.max(currentPage, 3) + 2, totalPages);
    } else {
      var start = Math.max(currentPage - 2, 1);
      // If we don't have a count, they are only allowed to go forward one page 
      // at a time, to avoid going "off the end".
      // The link for the next page is below.
      var end = currentPage;
    }
  %>
  <% if (start > 2) { %>
    <li><a href="#" data-page="1">1&hellip;</a></li>
  <% } else if (start == 2) { %>
    <li><a href="#" data-page="1">1</a></li>
  <% } %>
  <% for (var i = start; i <= end; i ++) { %>
    <% if (currentPage == i) { %>
      <li class="active"><span><%= i %></span></li>
    <% } else { %>
      <li><a href="#" data-page="<%= i %>"><%= i %></a></li>
    <% } %>
  <% } %>
  <% if (_.isNumber(totalPages)) { %>
    <% if (end < totalPages - 1) { %>
      <li><a href="#" data-page="<%= totalPages %>">&hellip;<%= totalPages %></a></li>
    <% } else if (end == totalPages - 1) { %>
      <li><a href="#" data-page="<%= totalPages %>"><%= totalPages %></a></li>
    <% } %>
  <% } else { %>
    <li><a href="#" data-page="<%= i %>"><%= i %>&hellip;</a></li>
  <% } %>
</ul>
