I m developing macro showing list of updated pages with little preview (similar as blog post list but without blog application) For that I would like to display first two paragraphs from the page. Is there simple way to do it?
Hi, yes it’s possible. When you have a document object ($doc
for example in velocity), you can call getXDOM()
to get the XDOM (see https://rendering.xwiki.org/xwiki/bin/view/Main/WebHome) and then you can find the first 2 ParagraphBlock
. See some example at https://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted#HExamples
You can call XDOM#getFirstBlock(...)
for example to get the first block of type ParagraphBlock
. You can check the XDOM API here: XWiki Rendering - Api 13.2 API
Hope it helps
2 Likes
thank you, that helped a lot.
Here is the code to get first paragraph of a page and display it.
{{velocity}}
#set ($doc = $xwiki.getDocument("samplePage.WebHome"));
#set ($xdom = $doc.getXDOM());
##find first paragraph
#set ($block = $xdom.getFirstBlock('class:ParagraphBlock','DESCENDANT'))
##render it and display it
#set ($xwikiSyntax = $services.rendering.render($block, "xwiki/2.1"))
$xwikiSyntax
{{/velocity}}
2 Likes