Using velocity macro as content (custom display) for a string property of some class

Hi,

I am trying to use a velocity macro as custom display for a string property of an class. Like this:

{{velocity}}
{{include reference="Intranet.ITDocsInfrastructure.Macros" /}}
#getLastComment($doc)
{{/velocity}}

Page “Intranet.ITDocsInfrastructure.Macros” contents:

{{velocity}}
#macro(getLastComment $itemdoc)
  #set($maxLen=120)
  #set($class="XWiki.XWikiComments")
  #set($name=$itemdoc.fullName)
  #set($xwlquery="select obj.comment from Document doc, doc.object($class) as obj where doc.fullName='$itemdoc.fullName' order by obj.date desc")
  #set($list=$services.query.xwql($xwlquery).setLimit(1).execute())
  #if($list.size()>0)
    #set($str=$list.get(0))
    #set($len=$str.length())
    #if($len>$maxLen)
      #set($str=$str.substring(0,$maxLen)+"...")
    #end
    #set($rez=$str)
  #else
    #set($rez="")
  #end
  $rez
#end
{{/velocity}}

This is a simple macro for getting last comment from a page. Problem, that I get as result:
#getLastComment($doc)” - but not a real comment. If I write macro text inside custom display directly - all is ok. What solution can be for this situation?


Thanks beforehand!
Eugen

Note: I’d have written it like this (haven’t checked your getLastComment method):

{{include reference="Intranet.ITDocsInfrastructure.Macros" /}}

{{velocity}}
#getLastComment($doc)
{{/velocity}}

You probably need to debug tour getLastComment macro and verify it works fine. You can comment out stuff till you have it working.

Maybe this can also help you: https://www.xwiki.org/xwiki/bin/view/FAQ/Bindings%20available%20inside%20the%20Custom%20Display%20property

The Velocity code (getLastComment) is evaluated before the wiki syntax (the include macro). Move the include outside of the Velocity macro as suggested by @vmassol .

Firstly I tried to check macro - all is ok.
If write #getLastComment($doc) directly on a page with macro - I gtet result - last comment.
As I understand, problem doesn’t linked with “custom display” of a property. I wrote as you and Vincent written:

{{include reference=“Intranet.ITDocsInfrastructure.Macros” /}}

{{velocity}}
#getLastComment($doc)
{{/velocity}}

  • same result - problem exists. Also I tried directly on a different page (inside Sandbox.TestPage3) - same result - problem exists.
    PS. I am using XWiki 8.4.4

Maybe this is not a terminal page and you need to write:

{{include reference=“Intranet.ITDocsInfrastructure.Macros.WebHome” /}}

Yes! you are right! It is not a terminal page. Problem, that I forgot WeHome. Thanks a lot!
Eugen