Home arrow Zope arrow Page 6 - DTML Basics (part 4)

The Real Thing - Zope

In this concluding article, dip your toes in the waters of more advanced DTML programming, with explanations of the , and tags.

  1. DTML Basics (part 4)
  2. With Or Without You
  3. A Polite Request
  4. Let Me In
  5. Climbing A Tree
  6. The Real Thing
  7. Call Of The Wild
  8. The Writing On The Wall
  9. Endzone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
July 17, 2002

print this article


How about building on the basic tree in the previous example, and making it a little more useful? This next variant allows you to add links to the terminal nodes so that the user can view the object by clicking it.

<dtml-tree branches_expr="objectValues()" sort="id"> <dtml-if "meta_type=='Folder'"> <img src="<dtml-var icon>"> <dtml-var getId> <dtml-else> <img src="<dtml-var icon>"> <a href="<dtml-var tree-item-url>"> <dtml-var getId> </a> </dtml-if> </dtml-tree>
Before I dissect this code, let's take a quick peek at the output:

Let's look at the code line-by-line to see how this happened.

<dtml-tree branches_expr="objectValues()" sort="id">
Nothing special here - I've used the objectValues() method to obtain a list of objects that are located in the current hierarchy, and the "sort" attribute to sort it by the object ID (in case all this is new to you, go back to the previous article in this series, refresh your memory and then continue reading).

I've also linked all the objects except the Folder objects, via the following "if-else" block:

<dtml-if "meta_type=='Folder'"> <img src="<dtml-var icon>"> <dtml-var getId> <dtml-else> <img src="<dtml-var icon>"> <a href="<dtml-var tree-item-url>"> <dtml-var getId> </a> </dtml-if>
The "meta-type" attribute specifies the type of object, and has been used as the decision variable for the "if-else" block in the code snippet above. This attribute can come in handy if you're not sure which object you're dealing with - try typing

I am a <dtml-var meta_type>.
into an empty DTML Document and viewing the output to see how it works.

<dtml-else> <img src="<dtml-var icon>"> <a href="<dtml-var tree-item-url>"> <dtml-var getId> </a> </dtml-if>
The hyperlinks themselves are accomplished via the special "tree-item-url" variable, which stores the URL for the current object. You're probably already familiar with the special "icon" variable, which stores the path to the corresponding object icon.

Apart from the "tree-item-url" variable, the <dtml-tree> construct also exposes a number of other interesting variables. These include the "tree-root-url" variable, which returns the absolute URL to the base of the tree; the "tree-level" variable, which specifies the depth of the current node; and the "tree-item-expanded" variable, which returns a Boolean value based on the state of the node (true if it has been expanded, false if it has been collapsed).

This last property can be easily included in the example above to indicate the folders which have been expanded. Take a look at the code:

<dtml-tree branches_expr="objectValues()" sort="id"> <dtml-if "meta_type=='Folder'"> <img src="<dtml-var icon>" > <dtml-if tree-item-expanded> <b><dtml-var getId></b> <dtml-else> <dtml-var getId> </dtml-if> <dtml-else> <img src="<dtml-var icon>"> <a href="<dtml-var tree-item-url>"> <dtml-var getId> </a> </dtml-if> </dtml-tree>
Here's the output:

If you take a close look at the output, you will see that expanded folders are displayed in bold.

How about the ability to expand or collapse the entire tree? Add this bit of code to the example above:

<p> <a href="<dtml-var URL0>?expand_all=1">Expand tree</a> | <a href="<dtml-var URL0>?collapse_all=1">Collapse tree</a> </p>

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: