Add links to (possibly) new pages in PocketQuery template

I've got a PocketQuery template which lists program names and descriptions as found in a database table.

I want the program name to be a link to a documentation page about that program, but the page may or may not exist.

I've tried the following so far:

$PocketQuery.renderWikiMarkup('[$row.program]')
$PocketQuery.renderXhtml('<ac:link>$row.program</ac:link>')
#contentLink2($row.program false false)

But none of these work.

Is there another way to do this? Is there documentation somewhere that gives this kind of information/examples?

3 answers

1 accepted

This widget could not be displayed.

Hi Duncan,

Since PQ templates are rendered in view mode, you can't use any editor features or XHTML content. But you can easily do it with Velocity programming.

If you simply only need the link with always the same text and no other logic, you can simply use a createpage link. If the page already exists, there will simply be a redirect to that page:

&lt;a href="${req.contextPath}/pages/createpage.action?spaceKey=spaceKeyOfPage&amp;title=${generalUtil.urlEncode($row.program)}&amp;linkCreation=true"&gt;$row.program&lt;/a&gt;

If you need some more logic, you can also do this and check if the page exists first:

#set($myLinkedPage = $pageManager.getPage("spaceKeyOfPage", $row.program))
#if ($myLinkedPage) ## page already exists
	&lt;a href="${req.contextPath}${myLinkedPage.getUrlPath()}"&gt;$row.program&lt;/a&gt;
#else ## page doesn't exist yet
	&lt;a href="${req.contextPath}/pages/createpage.action?spaceKey=spaceKeyOfPage&amp;title=${generalUtil.urlEncode($row.program)}&amp;linkCreation=true"&gt;Create page: $row.program&lt;/a&gt;
#end

Simply replace the spaceKeyOfPage in both cases with the key of the space where the page should be.

Does this help?

Regards, Felix (Scandio)

Thanks Felix, the first option you gave me works really well. You don't notice it redirecting to pages that already exist. By the way, I haven't answered your other comment on the Nested Queries question as I don't have enough rep on here to do more than 1 thing in 24 hours. Any help with that would be appreciated (voting on my questions?). Also, is there a way to get the space key in the velocity code? I tried using #set($spacekey = $content.getSpaceKey()) which I saw on another answer, but it doesn't seem to work.

Hi Duncan! I voted on all your questions ;). You should be able to obtain the spaceKey with $page.spaceKey

Thanks Felix, $page.spaceKey works! :-)

Thank you, I've been battling this one for nearly an hour thinking it was a velocity issue. 

This widget could not be displayed.

Hi Duncan! Thank you for your interest in PocketQuery! What exactly is $row.program? Is it an URL to a Confluence page? Do I understand you right that you want to display the link only if it is a valid page link?

This widget could not be displayed.

It is just the name of a program, like 'abcxyz'. I want to use that to have a link to a page in this space called 'abcxyz' that describes what that program does. But obviously we don't have pages yet for the 3,500 programs in our system, so the links need to point to pages that don't exist yet. Essentially I need the same functionality as if I have typed [abcxyz] in the editor. Confluence knows it's a link, but the page doesn't exist yet, and Confluence gives you a (different coloured) link to create that page. Does that make sense?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 17, 2018 in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

606 views 8 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you