Dashboard > Community Wiki > ... > Template Examples > Get a list of child pages
Get a list of child pages Log In View a printable version of the current page.

Added by GrĂ©gory Joseph , last edited by Boris Kraft on Mar 14, 2008  (view change)
Labels: 

I have a a news section on my site which contains navigation entries for every year. The individual news items however aren't shown. So I need the year page to show all its children in a list. After trying to solve the problem with JSTL, I gave in a wrote some Java code

There's a JSP page which can be included in other JSPs, and it puts four collections into the request:

  • titles, which contains the titles of every child
  • summaries, which contains the summaries of every child. Note that this is a node I created myself.
  • dates, which contains the dates of every child in the format dd.MM.yyyy (hard-coded for the moment)
  • handles, which contains the handles of every child, used to create a link to that page

I had to resort to JSTL for looping etc.

The JSP to include:

<%@ page import="info.magnolia.cms.core.Content,
				 java.util.Iterator,
				 java.util.Vector,
				 java.util.Calendar,
				 java.text.SimpleDateFormat,
				 info.magnolia.cms.core.HierarchyManager,
				 info.magnolia.cms.util.Resource"%>
				 
<%--
	This gets the active page's children pages and gets their data.
	From this data, it puts several collections into the request, so the calling JSP can use them:
	- 'titles' with the title of every child
	- 'summaries' with the summaries of every child. This may or may not exist!
	- 'dates' with the date of every child. This may or may not exist!
	- 'handles' with the handle of every child, which is used to create a link to that page
--%>

<%
        Content activePage = Resource.getActivePage(request);
        Vector dates = new Vector();
        Vector titles = new Vector();
        Vector summaries = new Vector();
        Vector handles = new Vector();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");

        Iterator childIterator = activePage.getChildren().iterator();
        while (childIterator.hasNext()) {
            Content childPage = (Content)childIterator.next();
            String dateString = childPage.getNodeData("date").getString();
            if (dateString.length() > 0) {
                Calendar date = childPage.getNodeData("date").getDate();
                dates.add(dateFormat.format(date.getTime()));
            }
            else {
            	dates.add(null);
            }
            titles.add(childPage.getNodeData("title").getString());
            summaries.add(childPage.getNodeData("summary").getString());
            handles.add(childPage.getHandle());
        }

        request.setAttribute("dates", dates);
        request.setAttribute("titles", titles);
        request.setAttribute("summaries", summaries);
        request.setAttribute("handles", handles);
%>

The JSP handling the layout

<%@ page import="org.apache.log4j.Logger"%>
<%@ taglib uri="cms-taglib" prefix="cms" %>
<%@ taglib uri="cms-util-taglib" prefix="cmsu" %>
<%@ taglib uri="JSTL" prefix="c" %>

<c:import url="/templates/jsp/include/head.jsp"/>

<div id="contentBody">
    <table>
        <thead>
            <tr>
                <th>
                    Datum
                </th>
                <th>
                    Titel
                </th>
            </tr>
        </thead>
        <tbody>
            <c:import url="/templates/jsp/include/children_pages.jsp"/>
            <c:forEach var="title" items="$\{titles}" varStatus="status">
                <c:set var="date" value="${dates[Get a list of child pages^status.index]}"/>
                <c:set var="handle" value="${handles[Get a list of child pages^status.index]}"/>
                <c:set var="summary" value="${summaries[Get a list of child pages^status.index]}"/>
                <tr>
                    <td>
                        <c:out value="$\{date}"/>
                    </td>
                    <td>
                        <a href="<c:out value="$\{handle}"/>"><c:out value="$\{title}"/></a>
                    </td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</div>

<c:import url="/templates/jsp/include/foot.jsp"/>
Powered by a free Atlassian Confluence Open Source Project License granted to Magnolia International. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators