It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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:

#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

2 votes
Answer accepted

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;

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. 

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?

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
Published in Confluence

2019: All Wrap(ped) Up Like a Present

Hindsight might be 2020 but looking back, a lot of cool new features rolled out in 2019. From new collaboration and organizational powers to fresh templates and handy integrations, it’s been quite th...

245 views 0 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you