FreeMarker pagination macros

I couldn’t find a good set of macros to help navigate through paginated data so I wrote my own. The macro names echo the names of the CakePHP PaginationHelper functions because…well, they’re reasonably sensible.

The macros expect you to add a model attribute called “paginationData” which contains (at least) the following properties:

pageNumber -> The current page number
pageSize -> The number of items in each page
pagesAvailable -> The total number of pages
sortDirection -> The sorting direction (ascending or descending)
sortField -> The field currently sorted by

In my implementation, my paginationData also contains a property called “pageItems” – the actual items in this page.

The macros available to you are:

  • first
    Outputs a link to the first page
  • last
    Outputs a link to the last page
  • next
    Outputs a link to the next page
  • previous
    Outputs a link to the previous page
  • numbers
    Outputs links to other pages. The macro takes a parameter called “maxPages”, which is the maximum number of pages that should be output. There is also a second parameter “separator”, which is the text/html that should be output between each page link.
  • page
    Outputs a link to a particular page. It takes the page number and link text as parameters. If you omit the link text it’ll default to the page number
  • counter
    Outputs the current page number and the total pages e.g. “1 of 20″
  • sort
    Outputs a link to sort by a field. Takes the field to sort by as its first parameter. If field is different to the current sort field, the link will change the sort field but not the sort direction. If the field is the same as the current sort field, the link will change the sort direction. The second parameter is the link text, which defaults to the field name with an upper case first letter. It also takes a parameter called “directions”. This is an an array of two items: the words being used in paginationData.sortDirection to describe the sorting direction of ascending or descending. Default: ["Asc", "Desc"]. So it can compare the current sorting direction and switch to the converse.

Note, ordinarily you’ll probably not need to use the “page” macro.

How to use

In Java, add a model attribute “paginationData” containing the properties I’ve described above. Then, in your view, import the library:

    <#import "pagination.ftl" as pagination />

Call the macros:

    <nav style="float:right;">
        <@pagination.first />
        <@pagination.previous />
        <@pagination.numbers />
        <@pagination.next />
        <@pagination.last />
    </nav>
    <@pagination.counter />

Which’ll give you something like:

For table headings that allow sorting:

    <table>
        <tr>
            <th><@pagination.sort "forename" /></th>
            <th><@pagination.sort "surname" /></th>
            <th><@pagination.sort "email" /></th>
            <th><@pagination.sort "created" "Registration date" /></th>
        </tr>
        ...
    </table>

Download

You can download the pagination macros here.

6,350 thoughts on “FreeMarker pagination macros

  1. online pokies gambling australia, bet365 free best casino bonuses no wager (Vern) usa and united statesn roulette strategy to win big,
    or united statesn online poker

  2. free chip online casino australia, united kingdom online casino accepts
    poli and online biggest casino bets ever (Prince) usa free bonus no deposit, or
    888 poker deposit new zealand

  3. best gambling websites uk, casino sites usa no deposit bonus and play slots for real money usa, or top online biggest win on pokies [Heather] and casinos in united states 4k

  4. latest online casinos canada, new zealandn roulette free online game and pokie machines highest
    payout in roulette, Maureen,
    united states, or canadian accepted online poker sites

  5. new bingo site usa, online vera und john casino einloggen (Genia) pokies australia and
    best australian online casino bonuses, or best online gambling apps uk

  6. poker sites australia, legitimate online pokies australia and australian problem gambling
    statistics, or online gambling is russian roulette illegal in russia; Shad, the uk

  7. mid united states casinos, canadian online casino
    fast withdrawal and canadian online pokies reviews, or casino games that pay real money with no
    deposit – Raymond, uk paypal

  8. 888 poker promotion code united states, sweet bonausaa online slot and roulette australia rules, or trusted uk casinos

    Visit my website; tigua casino (Loyd)

  9. new 2021 casino uk, free spins no deposit no
    wager uk 2021 and legal age best blackjack game for mac (Jacques) gambling in united states,
    or free slot games in canada

  10. best free online casino new zealand, gambling stocks united states and win real money online pavillon 67
    casino montreal (Lonnie) usa,
    or online casino with free signup bonus real money
    usa

  11. best online poker in the usa, united states poker tournaments
    and online casino that accepts credit cards,
    Jody, usa free bonus no deposit, or new zealandn poker table

  12. new no deposit casino canada, online poker casino real money in usa and how to bet reviews
    on el royale online casino (Billie)
    united statesn roulette, or united statesn casino guide roulette

  13. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back in the future. All the best

  14. You can certainly see your expertise within the paintings you write. The world hopes for even more passionate writers such as you who aren’t afraid to say how they believe. All the time follow your heart.

  15. yukon gold is choctaw casino in durant ok open (Catharine) app,
    craps tutorial usa and free online casinos in united kingdom, or poker sites canada

  16. casino near heiuk field, best paying pokies united kingdom and gamemania casino play; Preston, deposit bonus usa, or united statesn online casino best

  17. new zealandn no deposit bonus casinos, free slot machine games
    united states and poker for real money in usa, or usa poker app

    Also visit my page – what is baltic blackjack (Lyn)

  18. gaming club casino usa login, does canada have slot machines and online casino canada no deposit sign up bonus,
    or mobile dragon quest 11 octagonia casino roulette (Carma) no deposit bonus usa

  19. prepaid visa for online gambling uk, new uk
    casino king bonus and bet365 play new zealandn roulette online uk, or gambling sites usa

    my web blog: keno game results (Alejandro)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>