Classes: get pretty name of a property via velocity?

Hello,

im currently loosing my mind for at least two to three hours.
I simply want to get the pretty name of a property via velocity. However I can’t find a function for that.

Base:

#set ($xdoc=$xwiki.getDocument('XWiki.MyOwnTemplate'))
#set ($xobject = $xdoc.getObject('XWiki.MyOwnClass'))
#set ($xclass = $xobject.xWikiClass)
#set ($discard = $doc.use($xobject))

When using:

#foreach ($property in $xclass.properties)
  $property.Name
  $property.PrettyName
#end

I get both the Name (XWP_NAME) and the PrettyName (XWP_PRETTYNAME) of every property that the class have.

What I need however is a function to get the PrettyName of a Property. And where I start to loose my mind is this example:
$xobject.getProperty('a_Name').Name works and responds as expected with:

a_Name

However:
$xobject.getProperty('a_Name').PrettyName responds with:

$xobject.getProperty(‘a_Name’).PrettyName

What I need it for:
I have a larger script and inside I have an array of the property names (without references!!! It is just “coincidentally” exactly the property names) and with foreach through that Array I want to get the Pretty Names of the corresponding property in the xclass.

As the term / description “pretty name” is used so often inside XWiki, using google will throw like every result, but not once have I found something that was helping yet. :frowning:

I really don’t want to build something and check each individual non referenced property name for its matching property name within #foreach ($property in $xclass.properties) as this don’t seem like an “clean” solution to me.

Is there any function, something like $xclass.getPrettyName(“insert Property Name here”) ?

In order to get the prettyName for a property, you can call the $property.getPrettyName() method or use the velocity shortcut $property.prettyName.

But exactly that is not working for me.

When I use this code as example:

#set ($xdoc=$xwiki.getDocument('XWiki.MyOwnTemplate'))
#set ($xobject = $xdoc.getObject('XWiki.MyOwnClass'))
#set ($xclass = $xobject.xWikiClass)
#set ($discard = $doc.use($xobject))

$xobject.getProperty('a_Name')
##WORKS AND GIVES OUT THE REFERENCE (something like "com.xpn.xwiki.api.Property@4a98b986")

$xobject.getProperty('a_Name').name
##WORKS AND GIVES OUT THE PROPERTY NAME "a_Name"

#set ($property=$xobject.getProperty('a_Name'))
$property.getName()
##WORKS AND GIVES OUT THE PROPERTY NAME "a_Name"

$xobject.getProperty('a_Name').prettyName
##DOESN'T WORK AND GIVES OUT IT EXACT TEXT "$xobject.getProperty('a_Name').prettyName"

#set ($property=$xobject.getProperty('a_Name'))
$property.getPrettyName()
##DOESN'T WORK AND GIVES OUT IT EXACT TEXT "$property.getPrettyName()"

#set ($property=$xobject.getProperty('a_Name'))
$property.prettyName
##DOESN'T WORK AND GIVES OUT IT EXACT TEXT "$property.prettyName"

You can get if from the propoerty of the class and not of the object.

#set ($xobject = $doc.getObject('XWiki.XWikiUsers'))
#set ($xclass = $xobject.xWikiClass)
#foreach ($property in $xclass.properties)
  $property.prettyName
#end

But is there any other way with an direct function?

There is also $xclass.get('first_name').prettyName

1 Like

OMG thank you!! That works!

@TomTheWise your error was trying to get the pretty name from the xobject xproperty instead of from the xclass (which contains the property metadata).

BTW, it’s documented in an example at https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Scripting/APIGuide/#HAccessobjectsinapage

2 Likes