logo

MMBase tips

Related nodes

tip #13 / Thu 13 May 2010 / André / 0 comments

Revisiting the MyNews application, now including its related articles and how to display them with the mmbase taglib.

MMBase is all about nodes, but even more about their relations with other nodes. This example shows you how to do this in a template. Building upon the example of the tip #12 I have added an unordered list with a <mm:relatednodes /> tag.

<mm:node number="default.mags">
<h1><mm:field name="title" /></h1>
<mm:field name="intro" escape="paragraph" />

<ul>
<mm:relatednodes type="news"
role="posrel"
searchdir="destination">
<li><mm:field name="title" /></li>
</mm:relatednodes>
</ul>

</mm:node>

I have specified three attributes: type="news" meaning that I want related news nodes, role="posrel" which is the kind of relation by which the articles are attached to the magazine node, and searchdir="destination" a saveguard to prevent database queries traversing tables in both directions.
Withing the <mm:relatednodes /> the news node are available as normal nodes like in a <mm:node /> tag.

Relatednodes container

The example shows the articles titles in a rather unpredictable order. By using the attribute 'orderby' you can specify a field to use to order the articles, but it is generally considered wiser to use the <mm:relatednodescontainer /> since it provides more flexiblity building the query.

<mm:node number="default.mags"> 
<h1><mm:field name="title" /></h1>
<mm:field name="intro" escape="paragraph" />

<mm:relatednodescontainer type="news"
role="posrel"
searchdirs="destination">
<mm:sortorder field="date" direction="DOWN" />

<ul>
<mm:relatednodes>
<li>
<mm:field name="date"><mm:time format="dd-MM-yyyy" /> - </mm:field>
<mm:field name="title" />
</li>
</mm:relatednodes>
</ul>

</mm:relatednodescontainer>

</mm:node>

I have included a <mm:sortorder /> tag that uses the 'date' field of every news node to sort them in descending order. A <mm:relatednodescontainer /> has the same set of attributes as <mm:relatednodes /> except for 'searchdir' which became 'searchdirs' making it congruent with the other container tag.
It has the following result.

<h1>MyNews magazine</h1>
<p>This MyNews magazine is an example of news articles in a magazine.</p>
<ul>
<li>29-06-2000 - XML based MMBase distribution</li>
<li>26-06-2000 - Pinkpop was no problem for MMBase</li>
<li>25-05-2000 - Broadcaster EO chooses MMBase</li>
<li>12-05-2000 - MMBase Community Meeting</li>
<li>27-03-2000 - MMBase goes OpenSource</li>
</ul>

Complete example

To copy and paste.

<mm:content 
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:mm="http://www.mmbase.org/mmbase-taglib-2.0"
expires="120" postprocessor="none" type="text/html">
<jsp:output omit-xml-declaration="true" />
<mm:cloud>
<html>
<head>
<title>MyNews</title>
</head>
<body>

<mm:node number="default.mags">
<h1><mm:field name="title" /></h1>
<mm:field name="intro" escape="paragraph" />
<mm:relatednodescontainer type="news" role="posrel" searchdirs="destination">
<mm:sortorder field="date" direction="DOWN" />
<ul>
<mm:relatednodes>
<li>
<mm:field name="date"><mm:time format="dd-MM-yyyy" /></mm:field>
- <mm:field name="title" />
</li>
</mm:relatednodes>
</ul>
</mm:relatednodescontainer>
</mm:node>

</body>
</html>
</mm:cloud>
</mm:content>

Ordering articles yourself

MyNews Objectmodel of this example

The objectmodel of this example is rather simple and shown here. The 'posrel' relation has just one field named 'pos', an integer that can be used to order the news articles. In the above example I used 'date' to order the articles, but if you would like to influence the ordering yourself change the <mm:sortorder /> tag. The field attribute of <mm:sortorder /> contains the nodetype 'posrel' and after the dot the field 'pos' to use for ordering.

<mm:sortorder field="posrel.pos" direction="UP" />

The article related with position 1 will be on top, next the one with position 2 etc.

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm