scrivener_html v1.8.1 Scrivener.HTML View Source

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
  use 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.

For SEO related functions, see Scrivener.HTML.SEO (these are automatically imported).

Link to this section 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

Link to this section Functions

Link to this function

find_path_fn(entries, path_args) View Source

Link to this function

pagination_links(paginator) View Source

Link to this function

pagination_links(paginator, opts) View Source

Link to this function

pagination_links(conn, paginator, opts) View Source

Link to this function

pagination_links(conn, paginator, args, opts) View Source

Generates the HTML pagination links for a given paginator returned by Scrivener.

The default options are:

#{inspect @defaults}

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. The full list of available view_styles is here:

#{inspect @view_styles}

An example of the output data:

iex> Scrivener.HTML.pagination_links(%Scrivener.Page{total_pages: 10, page_number: 5}) |> Phoenix.HTML.safe_to_string()
"<nav><ul class=\"pagination\"><li class=\"\"><a class=\"\" href=\"?page=4\" rel=\"prev\">&lt;&lt;</a></li><li class=\"\"><a class=\"\" href=\"?\" rel=\"canonical\">1</a></li><li class=\"\"><a class=\"\" href=\"?page=2\" rel=\"canonical\">2</a></li><li class=\"\"><a class=\"\" href=\"?page=3\" rel=\"canonical\">3</a></li><li class=\"\"><a class=\"\" href=\"?page=4\" rel=\"prev\">4</a></li><li class=\"active\"><a class=\"\">5</a></li><li class=\"\"><a class=\"\" href=\"?page=6\" rel=\"next\">6</a></li><li class=\"\"><a class=\"\" href=\"?page=7\" rel=\"canonical\">7</a></li><li class=\"\"><a class=\"\" href=\"?page=8\" rel=\"canonical\">8</a></li><li class=\"\"><a class=\"\" href=\"?page=9\" rel=\"canonical\">9</a></li><li class=\"\"><a class=\"\" href=\"?page=10\" rel=\"canonical\">10</a></li><li class=\"\"><a class=\"\" href=\"?page=6\" rel=\"next\">&gt;&gt;</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.

Link to this function

raw_pagination_links(paginator, options \\ []) View Source

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, ellipsis: {:safe, "&hellip;"}]

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}]
iex> Scrivener.HTML.raw_pagination_links(%{total_pages: 20, page_number: 10}, first: ["←"], last: ["→"])
[{"<<", 9}, {["←"], 1}, {:ellipsis, {:safe, "&hellip;"}}, {5, 5}, {6, 6},{7, 7}, {8, 8}, {9, 9}, {10, 10}, {11, 11}, {12, 12}, {13, 13}, {14, 14},{15, 15}, {:ellipsis, {:safe, "&hellip;"}}, {["→"], 20}, {">>", 11}]

Simply loop and pattern match over each item and transform it to your custom HTML.