Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created April 11, 2018 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neenjaw/9235392cde6c1c0b258a248b397faa1c to your computer and use it in GitHub Desktop.
Save neenjaw/9235392cde6c1c0b258a248b397faa1c to your computer and use it in GitHub Desktop.
Pagination template implemented in EJS
<div class="row text-center">
<% if (pages && pages > 0) { %>
<ul class="pagination text-center">
<% if (current == 1) { %>
<li class="disabled"><a>First</a></li>
<% } else { %>
<li><a href="/campgrounds<%if(search){%>?search=<%=search%><%}%>">First</a></li>
<% } %>
<% if (current == 1) { %>
<li class="disabled"><a>«</a></li>
<% } else { %>
<li><a href="/campgrounds?page=<%= Number(current) - 1 %><%if(search){%>&search=<%=search%><%}%>">«</a></li>
<% } %>
<% var i = (Number(current) > 5 ? Number(current) - 4 : 1) %>
<% if (i !== 1) { %>
<li class="disabled"><a>...</a></li>
<% } %>
<% for (; i <= (Number(current) + 4) && i <= pages; i++) { %>
<% if (i == current) { %>
<li class="active"><a><%= i %></a></li>
<% } else { %>
<li><a href="/campgrounds?page=<%= i %><%if(search){%>&search=<%=search%><%}%>"><%= i %></a></li>
<% } %>
<% if (i == Number(current) + 4 && i < pages) { %>
<li class="disabled"><a>...</a></li>
<% } %>
<% } %>
<% if (current == pages) { %>
<li class="disabled"><a>»</a></li>
<% } else { %>
<li><a href="/campgrounds?page=<%= Number(current) + 1 %><%if(search){%>&search=<%=search%><%}%>">»</a></li>
<% } %>
<% if (current == pages) { %>
<li class="disabled"><a>Last</a></li>
<% } else { %>
<li><a href="/campgrounds?page=<%= pages %><%if(search){%>&search=<%=search%><%}%>">Last</a></li>
<% } %>
</ul>
<% } %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment