0

In Play Framework (v 2.3.7) (scala 2.11.5) I wish to pass a Call to a twirl template but I won't be able to calculate the params until after the template loads.

The use case is pagination on a table (with constraints and user defined search options). I want to pass a single call to the template and have it "do the math" (much as I'm not a fan of "programming" in a template - I've chosen to keep HTML generation out of direct scala code -- not sure yet if that is a great decision or not...)

The Call will be similar to this:

@routes.AController.list(constraint.id, pageNumber, pageLength)

The args are Long, Int, Int in case that is relevant.

I believe what I need is referred to as a partial function? (I could be way off base here)

How do I pass this to say:

pagination.scala.html 

And how would I subsequently call it inside the twirl template?

PS: I have no problems passing fully formed augments to templates -- I just want to wipe the arguments off the argument being passed (man that IS confusing...) and generate those at a later moment in time. Failing that I would have to pass a minimum of four arguments up front which gets ugly really fast.

Of course I wish to make this generic enough to be reusable but I think I can handle that once I understand the mechanics of how to pass the initial augment.

Techmag
  • 1,293
  • 11
  • 21

1 Answers1

0

I think that latest moment for generating pagination URIs is your view or maybe even better controller's action. Keep in mind that reverse routing in Play is type-save, so you would need to use some kind of reflection to generate generic links from level of the pagination.scala.html partial.

Instead you can prepare let's say a List of Strings in your controller and pass this to your pagination partial so it can render the typical pagination without any knowledge about the Call for given action.

Note: I like to create own PaginateUtil class which contains such fields like numberOfPages, currentPage, currentItems etc. and then passing it whole to the view, to make manipulation in it as small as possible.

biesior
  • 54,554
  • 10
  • 118
  • 177
  • I already have pagination working (from within the twirl template space), along with constraints and user definable search -- all I want to do now is put this into a more concise and reusable format. – Techmag Apr 21 '15 at 18:19