In my template I’m iterating over a list of elements. There are certain cases where I would like to break; or continue; in these loops. How would I do this in the play template language?
I’ve been checking the documentation here: http://www.playframework.org/documentation/1.0.2.1/tags but I can’t seem to find any information on how I would break or continue. Below are two example use cases:
#{list items:myList as:'my'}
#{if my.name == "hello"}
//Do something
//I'd like to break here.
#{/if}
#{/list}
also later on in my code I’ve got a nested list iteration like so…
#{list items:item, as:'i'}
#{list items:anotherItem, as:'aI'}
#{if i.name != aI.name}
//Do Something here.
//I'd like to continue here.
#{/if}
#{/list}
#{/list}
Continues
Just an additional question.. can I do things with a list like .contains();? Does anybody know of a nice guide for this language?
I think break and continue are not natively supported but you can always create your own tags to handle these cases. Take a look at http://www.playframework.org/documentation/1.1.1/templates#fasttags for details.
The default implementation of #list is GroovyInlineTags#_list – this should give you everything you need to customise it for extra arguments.
Personally, I would handle this logic on the server side and only pass into the view what you want to show. break is then no longer needed, and continue can be implemented as an if-else statement using the existing tags.
As for calling methods on objects, certainly. You can use, for example, #{if myList.contains(‘whatever’)} or even put Groovy elvis operator in there for null-safety, e.g. #{if myList?.contains(‘whatever’)}
Incidentally, which version of the Play framework are you using? 1.0.2 is pretty old now! 1.1.1 is the latest version of the 1.1 branch, and 1.2.4 is the latest version of the trunk. There’s also Play 2 (http://www.playframework.org/2.0) which is good for experimentation but you may want to hold off on using it for production for a while. Later versions of the framework have much better documentation. When running in dev mode, you can also access the version-specific documentation at http://localhost:9000/@documentation (correct the port number as necessary).