Velocity Model and Data Objects in Apache Roller

Exclusive offer: get 50% off this eBook here
Apache Roller 4.0 – Beginner's Guide

Apache Roller 4.0 – Beginner's Guide — Save 50%

A comprehensive, step-by-step guide on how to set up, customize, and market your blog using Apache Roller

$26.99    $13.50
by | December 2009 | Beginner's Guides Content Management Java Open Source

In this article by Alfonso V. Romero, we'll see some of the Velocity elements used in Roller to display data from your weblog and work with Roller's model and data objects to access your weblog's data from custom templates.

To know more about $model object and $url read Working with Templates in Apache Roller 4.0.

Velocity model and data objects in Apache Roller

There are several standard model and data objects you can use in Apache Roller to access your weblog's data. Some of the most widely used are $config, $model, $url, $utils, $weblog, $weblogCategory, and $weblogPage. In the following exercise, you'll get to work with the $config object.

The $config object

This model is used to access Roller's site-wide configuration parameters, such as the e-mail address of Roller's administrator, the name of the Roller site, and so on.

Time for action – properties of the $config model object

The following exercise will show you how to use some properties of the $config model object inside your custom template:

  1. Go to your custom template editing page, and replace the six lines of code below the <body><html> line with the following lines:
    Welcome to my blog, <b>$model.weblog.name</b> --> <i>$config.
    siteDescription</i> </br>
    I'm using Apache Roller Version <b>$config.rollerVersion</b> </br>
    You can e-mail me at <b>$config.siteEmail</b> if you run into any
    problems in this site. </br>
    This is my first template </br>
    My weblog's absolute URL is: <b>$url.absoluteSite</b> </br>
    </br>
  2. The entire code of your template should look like the following screenshot (the code you have to change is highlighted):

    Apache Roller 4.0 – Beginner's Guide

  3. Save your changes and click on your template's [launch] link to open a new tab in your web browser and see the results:

    Apache Roller 4.0 – Beginner's Guide

  4. Close the results tab if you like, but leave the Frontpage: Templates window open for the next exercise.

What just happened?

The following table summarizes the $config object properties you learned to use in the previous exercise, along with their value:

Property

Definition

Value

$config.siteDescription

Shows your site's description.

My First Installation of Apache Roller

$config.rollerVersion

Shows the Apache Roller version used.

4.0.1

$config.siteEmail

Shows the site administrator's email.

alromeromx@gmail.com

You can find all $config parameters under Roller's Server Admin tab:

The $model, $category, and $entry objects

These objects are used to access all the data for a specific weblog: weblog entries, categories, comments, among others.

Apache Roller 4.0 – Beginner's Guide A comprehensive, step-by-step guide on how to set up, customize, and market your blog using Apache Roller
Published: December 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

Time for action – properties and methods of the $model model object

The following exercise will show you how to use some properties and methods of the $model object inside your custom template, along with some other data objects such as $weblog, $category, and $entry:

  1. Go to your custom template editing page, and replace the last three lines of code above the </body></html> line with the following lines:
    And these are the 5 most recent entries from each category in my
    weblog: </br>
    <table border="1">
    <tr><td><b>Category</b></td><td><b>Entries</b></td></tr>
    #foreach ($category in $model.weblog.getWeblogCategory("nil").
    weblogCategories)
    <tr><td><i>$category.name</i></td>
    <td>
    #foreach($entry in $model.weblog.getRecentWeblogEntries("$category
    .name", 5))
    <a href="$entry.permalink">$entry.title</a><br>
    #end
    </td></tr>
    #end
    </table>

  2. The entire code of your template should look like the following screenshot (the code you have to change is highlighted):

    Apache Roller 4.0 – Beginner's Guide

  3. Save your changes and click on your template's [launch] link to open a new tab in your web browser and see the results:

    Apache Roller 4.0 – Beginner's Guide

  4. Now close the results tab, select all the HTML code from your custom template, and save it in a text file for later use. You can log out from Roller and close your web browser for now.

What just happened?

The following table summarizes the $model, $weblog, $category, and $entry objects' properties that you learned to use in the previous exercise, along with their definition:

Property

Definition

$model.weblog.getWeblogCategory("nil").weblogCategories

Gets a list of all the categories within your weblog

$category.name

Displays the name of a category

$model.weblog.getRecentWeblogEntries("$category.name", 5))

Gets the 5 most recent entries from the category defined by $category.name

$entry.permalink

Displays the permalink of an entry

$entry.title

Displays the title of an entry

In this exercise there are also some Velocity instructions involved:

#foreach ($category in $model.weblog.getWeblogCategory("nil").weblogCategories)
<tr><td><i>$category.name</i></td>
<td>
#foreach($entry in $model.weblog.getRecentWeblogEntries("$category.name", 5))
<a href="$entry.permalink">$entry.title</a><br>
#end
</td></tr>
#end
</table>

There are two foreach loops, one nested inside the other, to get all of the entries for each one of your weblog's categories. The first foreach loop iterates through each category obtained through the getWeblogCategory method, and prints the name of each category via the $category.name property. The second foreach loop iterates through each entry obtained through the getRecentWeblogEntries method, and prints the title of each entry via the $entry.title property, along with its permalink via the $entry.permalink property. The <a href> HTML tag converts the entry's permalink into a hyperlink, as shown in step 3's screenshot

A foreach loop is a programming language construct for traversing items or elements in a collection of some kind; in this case, each category of your Roller blog is an item, and all the categories as a whole represent the collection

The $url and $utils objects

These objects have several properties and methods you can use in your templates to display URLs and format data elements, such as the date and time of publication of an entry in your weblog.

Time for action – properties and methods of the $url and $utils objects

The following exercise will show you how to use some properties and methods of the $url, $utils, $category, and $entry objects inside your custom template:

  1. Go to your custom template editing page, and replace the

    <tr><td><i>$category.name</i></td> line with the following line: <tr><td><i><a href="$url.category($category.path)">$category. name</a></i></td></p></li>

  2. The following screenshot shows how your code will look like after the change:

    Apache Roller 4.0 – Beginner's Guide

  3. Save your changes and click on your template's [launch] link to open a new tab in your web browser and see the results:

    Apache Roller 4.0 – Beginner's Guide

  4. If you click on any of the categories, Roller will show you all the posts within the category you chose. For example, if you click on the General category, you'll get the following results:

    Apache Roller 4.0 – Beginner's Guide

  5. Now go back to your custom template's editing page and replace <ahref="$entry.permalink">$entry.title7lt;/a><br> with the following line:<a href="$entry.permalink">$entry.title</a> -> <a href="$url.editEntry($entry.anchor)"> Edit Entry</a><br>
  6. The following screenshot shows how your custom template's code will look after changing the previous line:

    Apache Roller 4.0 – Beginner's Guide

  7. Save your changes and click on the [launch] link to see how your custom template will look after the changes:

    Apache Roller 4.0 – Beginner's Guide

  8. Click on any of the Edit Entry links and Roller will take you to the editing page for that corresponding entry. The following screenshot shows the edit page for the Relaxing sounds entry:

    Apache Roller 4.0 – Beginner's Guide

  9. Close the Front Page: Edit Entry tab and return to your custom template's editing page. Now replace the
     <a href="$entry.permalink">$entry.
    title</a> -> <a href="$url.editEntry($entry.anchor)">
    Edit Entry</a><br>

    line with the following code:

    <a href="$entry.permalink">$entry.title</a> -> <a href="$url.
    editEntry($entry.anchor)"> Edit Entry</a> --> $entry.pubTime<br>

  10. The following screenshot shows how your custom template's code will look after changing the previous line:

    Apache Roller 4.0 – Beginner's Guide

  11. Save your changes and click on [launch] to see the results:

    Apache Roller 4.0 – Beginner's Guide

  12. As you can see, this last modification to your custom template code shows the creation date and time of each entry. In order to change the date and time format, replace the

    <a href="$entry.permalink">$entry.title</a> 
    -> <a href="$url.editEntry($entry.anchor)">
    Edit Entry</a>
    --> $entry.pubTime<br>

    line with the following line:

    <a href="$entry.permalink">$entry.title</a> -> <a href="$url.
    editEntry($entry.anchor)"> Edit Entry</a> --> $utils.
    formatDate($entry.pubTime, "EEE, d MMM yyyy HH:mm:ss Z")<br>
  13. Your custom template code should look like this:

    Apache Roller 4.0 – Beginner's Guide

  14. Save your changes and click on the [launch] link to see the results:

    Apache Roller 4.0 – Beginner's Guide

  15. You can log out from Roller and close your web browser.

What just happened?

OK, now you've seen some of the things you can do with templates. The following table summarizes all the properties and methods from the $url and $utils objects you learned to use in the previous exercise:

Property

Definition

Tip

$category.path

Contains the absolute path of a category

Use it with the $url.category property to display the URL for a specific category

$url.category

Displays the URL of a specific category from your weblog

Use it with the $category.path property to display the URL for a specific category

$entry.anchor

Contains the anchor that uniquely identifies each entry in your weblog

Use it with the $url.editEntry property to create a URL for editing an entry

$url.editEntry

Takes you to the edit page of a specific entry

Use it with the $entry.anchor property to create a URL for editing an entry

$entry.pubTime

Displays the publication timestamp of an entry

Use it to show the exact date and time of publication of an entry in your weblog

$utils.formatDate

Formats a date object according to the format specified

Use it with the $entry.pubTime property and a date format string to change the way of showing the publication date & time of an entry

Summary

In this article,we covered Roller's model and data objects, and how to use these objects' properties, methods, and macros in a custom template to show data from your weblog.

Apache Roller 4.0 – Beginner's Guide A comprehensive, step-by-step guide on how to set up, customize, and market your blog using Apache Roller
Published: December 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


Books From Packt

Joomla! with Flash
Joomla! with Flash

Joomla! 1.5 SEO
Joomla! 1.5 SEO

Zend Framework 1.8 Web Application Development
Zend Framework 1.8 Web Application Development

Ext JS 3.0 Cookbook
Ext JS 3.0 Cookbook

ICEfaces 1.8: Next Generation Enterprise Web Development
ICEfaces 1.8: Next Generation Enterprise Web Development 

Spring Persistence with Hibernate
Spring Persistence with Hibernate 

3D Game Development with Microsoft Silverlight 3: Beginner's Guide
3D Game Development with Microsoft Silverlight 3: Beginner's Guide

Microsoft Office Live Small Business: Beginner’s Guide
Microsoft Office Live Small Business: Beginner’s Guide

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
C
g
Y
M
H
r
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software