In this example we will provide the content of the file news.xml to your website. At first make sure that you have already installed Logabit WebTools as described in the Install section.
At first let's create a new content document in $YOUR_APP/content/news.xml and then add the follwing lines to it:
<news>
<article>
<header>A cool news article</header>
<body>This is the body of the article</body>
</article>
<article>
<header>Another article</header>
<body>This is the body of the second article</body>
</article>
</news>
Note that there is no regulation on how to structure your content xml document. This structure is what we want.
After you have created the content xml document lets tell the layout news in your patchwork configuration that it should use this content xml document. This can be done in two different ways. Let's see an example on how to do that the first way:
... <layout name="news" extends="abstract.default" callable="true"> <include name="main" layout="abstract.news"/> <content name="news" type="xml" src="content/news.xml"/> <layout> ...
In the example above you can see that a new element <content/> was introduced. This element placed within <layout/> tells Patchwork that beside resolving the layout, it should also load the content defined here. Defining the content within the element <layout/> is called a local content definition. This means this content object can be used only locally within this layout definition.
If you have a content object that must be used from within different layouts you can define that content as a global content definition and then refer to it using the attribute ref as the example below shows:
... <!-- A global content definintion --> <content name="news" type="xml" src="content/news.xml"/> <layout name="news" extends="abstract.default" callable="true"> <include name="main" layout="abstract.news"/> <!-- Refer to the global content definition --> <content ref="news"/> <layout> ...
The last step would be to access the content from within your template and to format it to fit your design needs. Let's see the template news.jxt as an example:
<jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
<jx:set var="content" value="${request.getAttribute('content')}"/>
<jx:set var="newsDocument" value="#{$content/news}" />
<jx:forEach select="#{$newsDocument/news/articles}">
<table>
<tr>
<td>#{./header}</td>
</tr>
<tr>
<td>#{./body}</td>
</tr>
</table>
</jx:forEach>
</jx:template>
Contentwork places all content objects using their name into the bean content. This bean is called the content holder. The content holder itself can be retrieved from within any layer that has access to the current request scope. In JXTemplate for example you can do that this way:
<jx:set var="content" value="${request.getAttribute('content')}"/>
Once having the content holder you can access the required content object by its name from the content holder. Again an example on how to do that in JXTemplate:
<jx:set var="newsDocument" value="#{$content/news}" />
And then read the content of the document for example by iterating over all articles as shown in the example above.