defmodule FlopPhoenix do @moduledoc """ View helper functions for Phoenix and Flop. ## Pagination `Flop.meta/3` returns a `Flop.Meta` struct, which holds information such as the total item count, the total page count, the current page etc. This is all you need to render pagination links. `Flop.run/3`, `Flop.validate_and_run/3` and `Flop.validate_and_run!/3` all return the query results alongside the meta information. If you set up your context as described in the [Flop documentation](https://hexdocs.pm/flop), you will have a `list` function similar to the following: @spec list_pets(Flop.t() | map) :: {:ok, {[Pet.t()], Flop.Meta.t}} | {:error, Changeset.t()} def list_pets(flop \\ %{}) do Flop.validate_and_run(Pet, flop, for: Pet) end ### Controller You can call this function from your controller to get both the data and the meta data and pass both to your template. defmodule MyAppWeb.PetController do use MyAppWeb, :controller alias Flop alias MyApp.Pets alias MyApp.Pets.Pet action_fallback MyAppWeb.FallbackController def index(conn, params) do with {:ok, {pets, meta}} <- Pets.list_pets(params) do render(conn, "index.html", meta: meta, pets: pets) end end end ### View To make the `FlopPhoenix` functions available in all templates, locate the `view_helpers/0` macro and add another import statement: defp view_helpers do quote do # ... import FlopPhoenix # ... end end ### Template In your index template, you can now add pagination links like this:

Listing Pets

# ...
<%= pagination(@meta, &Routes.pet_path/3, [@conn, :index]) %> The second argument of `FlopPhoenix.pagination/4` is the route helper function, and the third argument is a list of arguments for that route helper. If you want to add path parameters, you can do that like this: <%= pagination(@meta, &Routes.owner_pet_path/4, [@conn, :index, @owner]) %> ## Customization `FlopPhoenix` sets some default classes and aria attributes. You can customize the css classes and add additional HTML attributes. It is recommended to set up the customization once in a view helper module, so that your templates aren't cluttered with options. Create a new file called `views/flop_helpers.ex`: defmodule MyAppWeb.FlopHelpers do use Phoenix.HTML def pagination(meta, route_helper, route_helper_args) do opts = [ # ... ] FlopPhoenix.pagination(meta, route_helper, route_helper_args, opts) end end Change the import in `my_app_web.ex`: defp view_helpers do quote do # ... import MyAppWeb.FlopHelpers # ... end end ## Attributes and CSS classes You can overwrite the default attributes of the `