On 03/11/2011 10:43 PM, Mark Wallace wrote:
I am having a velocity script problem with a class sheet when I try to display an empty list on an object.
My goal is for the class sheet to render class properties that start with a "_" as hot-links, and those that don't to just be rendered as text.
The class sheet is for "PersonClass", and looks like this:
{{velocity}} #set ($obj = $doc.getObject('XWiki.PersonClass')) #set($class = $obj.xWikiClass) #foreach($prop in $class.properties) ; $prop.prettyName #set ($propName = $prop.getName()) #set ($propVal = $obj.get($propName) ) #if ($propName.startsWith("_")) : $propVal (why does this one not show?) : [[$propVal>>$propVal]] (but this one does show?) #else : $doc.display($prop.getName()) #end #end {{/velocity}}
The PersonClass has two properties:
(1) name (a string)
(2) _knows (a DatabaseList with multiple select and XWiki Class Name of XWiki.PersonClass)
I create an object using this class, with "name" set to "C S Lewis" and with no selection for the _knows field (empty list).
The problem is, the velocity line:
: $propVal (why does this one not show?)
works properly... not displaying anything for property _knows (empty list), but the line:
: [[$propVal>>$propVal]] (but this one does show?)
The link syntax is different. If $propVal is the empty string, then the link becomes: [[>>]] If you look at the specification of the syntax: http://platform.xwiki.org/xwiki/bin/Main/XWikiSyntax#HXWikiSyntax2.0LinkSpec... you'll see that the first part is the label, which is optional, and, if missing, falls back on the name of the target document. So something will appear there. The second part is the name of the target document, which is resolved as a reference relative to the current document. "Resolved" means that any missing part of the reference, "wiki", "space" or "document", is replaced with a default from "current wiki", "current space", "current document". So, in fact your link translates to: [[xwiki:Space.C S Lewis]] which prints the name of the document, C S Lewis. You're not the first user surprised by this behavior, maybe we should change it? Anyway, a quick fix would be: : #if ("$!propVal" != '')[[$propVal>>$propVal]]#end <leave a blank line here since #end eats the newline above>
which is the line I really want to use in the class sheet, because it creates the hot links, shows the value from the previous property as a hot link. I.e. it looks like this:
name C S Lewis
_knows (why does this one not show?) C S Lewis (but this one does show?)
Not what I wanted. Why does it do this? How can I get empty lists to show no page links, but lists with one or more items to display them as a bunch of hot links?
Thanks, -Mark
-- Sergiu Dumitriu http://purl.org/net/sergiu/