Add links to (possibly) new pages in PocketQuery template
Asked by Duncan Kinnear:

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?


Answers:
author: Felix Grund (Scandio)
body: 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)

comments: author: Duncan Kinnear
body: 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.


author: Felix Grund (Scandio)
body: Hi Duncan! I voted on all your questions ;). You should be able to obtain the spaceKey with $page.spaceKey


author: Duncan Kinnear
body: Thanks Felix, $page.spaceKey works! :-)


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


author: Duncan Kinnear
body: 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?


author: Felix Grund (Scandio)
body: 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?


Tags: link

pocketquery