C:forEach

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
This tag is of course documented in a lot of other places, but since it is used extensively for openCMS development, I feel taht it has its use to be put here also.
+
This tag is not natice open CMS but the part of the JSTL core library. It is of course documented in a lot of other places, but since it is used extensively for openCMS development, I feel that it has its use to be put here also.
  
If you have included the jstl core tag lib like this:
+
If you have included the JSTL core tag lib like this:
 
<code lang="html4strict">
 
<code lang="html4strict">
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Line 23: Line 23:
 
*first
 
*first
 
*last
 
*last
*begin
+
*begin - Element to start with (0 = first item, 1 = second item, ...)
*step
+
*step - Precess every step item
*end
+
*end - Element to end with (0 = first item, 1 = second item, ...)
  
 
'''Example:'''
 
'''Example:'''
Line 34: Line 34:
 
</code>
 
</code>
  
 +
Iterating 5 to 10 item:
 +
<code lang="html4strict">
 +
<c:forEach var="yourItemOfCollectionVarName" items="${yourCollection}" begin="4" end="9" varStatus="yourStatus">
 +
  <div id="line-${yourStatus.index}">${yourItemOfCollectionVarName.name}</div>
 +
</c:forEach>
 +
</code>
 +
 +
'''Sources'''
 +
 +
tutorialspoint.com/jsp/jstl_core_foreach_tag.htm
  
 
[[Category:Developing in OpenCms ]]
 
[[Category:Developing in OpenCms ]]

Revision as of 17:43, 19 May 2013

This tag is not natice open CMS but the part of the JSTL core library. It is of course documented in a lot of other places, but since it is used extensively for openCMS development, I feel that it has its use to be put here also.

If you have included the JSTL core tag lib like this:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

you can use the c:forEach tag

<c:forEach var="yourItemOfCollectionVarName" items="${yourCollection}" varStatus="yourStatus">
   <!-- your code goes here -->
</c:forEach>

varStatus

If you like to do something different for every x row or only for the last or first row, varStatus is helping a lot. The optional varStatus variable has some useful properties to help you on this:

  • current
  • index
  • count
  • first
  • last
  • begin - Element to start with (0 = first item, 1 = second item, ...)
  • step - Precess every step item
  • end - Element to end with (0 = first item, 1 = second item, ...)

Example:

<c:forEach var="yourItemOfCollectionVarName" items="${yourCollection}" varStatus="yourStatus">
   <div id="line-${yourStatus.index}">${yourItemOfCollectionVarName.name}</div>
</c:forEach>

Iterating 5 to 10 item:

<c:forEach var="yourItemOfCollectionVarName" items="${yourCollection}" begin="4" end="9" varStatus="yourStatus">
   <div id="line-${yourStatus.index}">${yourItemOfCollectionVarName.name}</div>
</c:forEach>

Sources

tutorialspoint.com/jsp/jstl_core_foreach_tag.htm

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox