Frank Zinner bio photo

Frank Zinner

I'm a passionated senior software developer ...

Twitter Facebook LinkedIn Github Xing

Play2 - Iterating over images in Play2 templates with the Asset-Controller

You can iterate over images in Play2 templates whith the help of Tuples and the zipWithIndex function like this:


	@for((i, index) <- (1 to 6).zipWithIndex; image = "image" + (index + 1) + ".jpg") {     
		@if (index == 0) { 
			<img src="@routes.Assets.at("images/pfeil-links.gif")" alt=""/> 
		}  
		<img src="@routes.Assets.at("images/" + image)" alt="Hintergründe"/>                
		@if (index == 5) { 
			<img src="@routes.Assets.at("images/pfeil-rechts.gif")" alt=""/> 
		} 
	}                                                                                       

zipWithIndex gives you a Tuple with an index starting at 0. In the comprehensive-for-loop you can use the index in the asset-path to concatenate images. The path can be used like this:

@routes.Assets.at("path-to-images/" + variable)