Scrivener.HTML
For use with Phoenix.HTML, configure the :routes_helper
module like the following:
config :scrivener_html,
routes_helper: MyApp.Router.Helpers
Import to you view.
defmodule MyApp.UserView do
use MyApp.Web, :view
import Scrivener.HTML
end
Use in your template.
<%= pagination_links @conn, @page %>
Where @page
is a %Scrivener.Page{}
struct returned from Repo.paginate/2
.
Customize output. Below are the defaults.
<%= pagination_links @conn, @page, distance: 5, next: ">>", previous: "<<", first: true, last: true %>
See Scrivener.HTML.raw_pagination_links/2
for option descriptions.
For custom HTML output, see Scrivener.HTML.raw_pagination_links/2
.
Summary
Functions
Generates the HTML pagination links for a given paginator returned by Scrivener
Returns the raw data in order to generate the proper HTML for pagination links. Data
is returned in a {text, page_number}
format where text
is intended to be the text
of the link and page_number
is the page it should go to. Defaults are already supplied
and they are as follows:
Functions
Generates the HTML pagination links for a given paginator returned by Scrivener.
The default options are:
[view_style: :bootstrap]
The view_style
indicates which CSS framework you are using. The default is
:bootstrap
, but you can add your own using the Scrivener.HTML.raw_pagination_links/2
function
if desired.
An example of the output data:
iex> Scrivener.HTML.pagination_links(%Scrivener.Page{total_pages: 10, page_number: 5})
{:safe,
["<nav>",
["<ul class="pagination">",
[["<li>", ["<a class="" href="?page=4">", "<<", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=1">", "1", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=2">", "2", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=3">", "3", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=4">", "4", "</a>"], "</li>"],
["<li>", ["<a class="active" href="?page=5">", "5", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=6">", "6", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=7">", "7", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=8">", "8", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=9">", "9", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=10">", "10", "</a>"], "</li>"],
["<li>", ["<a class="" href="?page=6">", ">>", "</a>"], "</li>"]],
"</ul>"], "</nav>"]}
In order to generate links with nested objects (such as a list of comments for a given post)
it is necessary to pass those arguments. All arguments in the args
parameter will be directly
passed to the path helper function. Everything within opts
which are not options will passed
as params
to the path helper function. For example, @post
, which has an index of paginated
@comments
would look like the following:
Scrivener.HTML.pagination_links(@conn, @comments, [@post], view_style: :bootstrap, my_param: "foo")
You’ll need to be sure to configure :scrivener_html
with the :routes_helper
module (ex. MyApp.Routes.Helpers) in Phoenix. With that configured, the above would generate calls
to the post_comment_path(@conn, :index, @post.id, my_param: "foo", page: page)
for each page link.
In times that it is necessary to override the automatic path function resolution, you may supply the
correct path function to use by adding an extra key in the opts
parameter of :path
.
For example:
Scrivener.HTML.pagination_links(@conn, @comments, [@post], path: &post_comment_path/4)
Be sure to supply the function which accepts query string parameters (starts at arity 3, +1 for each relation),
because the page
parameter will always be supplied. If you supply the wrong function you will receive a
function undefined exception.
Returns the raw data in order to generate the proper HTML for pagination links. Data
is returned in a {text, page_number}
format where text
is intended to be the text
of the link and page_number
is the page it should go to. Defaults are already supplied
and they are as follows:
[distance: 5, next: ">>", previous: "<<", first: true, last: true]
distance
must be a positive non-zero integer or an exception is raised. next
and previous
should be
strings but can be anything you want as long as it is truthy, falsey values will remove
them from the output. first
and last
are only booleans, and they just include/remove
their respective link from output. An example of the data returned:
iex> Scrivener.HTML.raw_pagination_links(%{total_pages: 10, page_number: 5})
[{"<<", 4}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}, {10, 10}, {">>", 6}]
Simply loop and pattern match over each item and transform it to your custom HTML.