Hi Vidal,

Am Donnerstag, den 13.03.2008, 14:34 +0100 schrieb Vidar Ramdal:
> On 3/13/08, Bertrand Delacretaz <[EMAIL PROTECTED]> wrote:
> > On Thu, Mar 13, 2008 at 1:54 PM, Felix Meschberger <[EMAIL PROTECTED]> 
> > wrote:
> >  >  >... Also, is there any documentation somewhere, on the variables that 
> > are
> >
> > >  > available for scripting?
> >  >  The only "documentation" so far is the constants defined in the
> > >  SlingBindings class....
> >  And the tests: the .esp and .ecma scripts under
> >  
> > http://svn.apache.org/repos/asf/incubator/sling/trunk/launchpad/webapp/src/test/resources/integration-test/
> >
> >  provide some additional examples.
> 
> Thank you both, that's helpful.
> 
> Now, let's say I have a child iterator:
> var iterator = resource.resourceResolver.listChildren(r);
> 
> ... and I know that the children will be JCR nodes, how can I output
> (e.g.) the title attribute of all child nodes?
> 
> As you may have suspected, I'd want to produce a menu, like
> <ul>
>   <li><a href="child1">Title of child 1</a></li>
>   <li><a href="child2">Title of child 2</a></li>
>  ...
> </ul>

You have two options : (1) Stay with Resources or (2) access the node.

To stay with resources you may do for each child resource:

        var child = ...
        var title = resourceResolver.getResource(child, "title");
        if (title != null) {
           var titleString = title.adaptTo(java.lang.String);
        }

the trick here is, that the title resource represents a property and
with the adaptTo method you get the string value of that property.

The second option - using the node - you do

        var child = ...
        var childNode = child.adaptTo(Packages.javax.jcr.Node);
        if (childNode != null && childNode.hasProperty("title")) {
            var titleString =
        childNode.getProperty("title").getString();
        }

I personally prefer the first approach because it stays within a single
paradigm and you do not have to care about RepositoryExceptions being
thrown by directly accessing the node. Your mileage may vary, though.

Regards
Felix

> 
> 

Reply via email to