xen:link guide

Ryan Kent

Well-known member
Is there any basic tutorial or guide available describing what xen:links are and how to use them?
 
It's best to look at examples in the default templates.

The basic form is this:

Code:
<a href="{xen:link 'ROUTE'}">TEXT</a>

For example, this will link to the user upgrade page:

Code:
<a href="{xen:link account/upgrades}">{xen:phrase account_upgrades}</a>

The corresponding URL would be:

http://xenforo.com/community/account/upgrades/

It takes the route and builds it into a hyperlink.

Other types of links take additional parameters. For example, thread links are like this:

Code:
<a href="{xen:link 'threads', $thread}">Link to thread</a>

The handler extracts the appropriate information from the thread record to build the link.
 
Very helpful as always, thanks Jake.

So if I wanted to link to this thread years ago I would do it as:
Code:
<a href="http://xenforo.com/community/threads/xen-link-guide.15615">Link Guide</a>

If I want to link to it in XF a cleaner way to do it would be:
Code:
[a href="{xen:link 'threads', 15615}"> {xen:phrase link_guide}</a>

Is that correct? If so, what is the overall advantage outside of shorter code?
 
The {xen:link ...} syntax is not really a shorter form, it's a function call to the XenForo template system to build the link requested.

You can only use {xen:link ...} within templates, and all links to XenForo pages within XenForo templates should use {xen:link ...} rather than the http://... output, as this makes the links future-proof and responsive to option changes.
 
Thx Kier.

One additional question. I noticed an example of code Jake shared which shows parameters for a xen:link

Code:
<li><a href="{xen:link 'find-new/threads', '', 'days=30'}">Latest Threads</a></li>

What parameters does xen:link accept?
 
Very helpful as always, thanks Jake.

So if I wanted to link to this thread years ago I would do it as:
Code:
<a href="http://xenforo.com/community/threads/xen-link-guide.15615">Link Guide</a>

If I want to link to it in XF a cleaner way to do it would be:
Code:
[a href="{xen:link 'threads', 15615}"> {xen:phrase link_guide}</a>

Is that correct? If so, what is the overall advantage outside of shorter code?

See this post by Kier:

http://xenforo.com/community/thread...-i-only-have-the-thread-id.14177/#post-185595
 
This is sort of an old thread but i ended up searching trying to find an answer:
What is the xen:link to link our own profile to all users click an go their profile?
 
I think checking for the presence of user_id is enough without the comparison operator.

Yeah, probably. My employers prefer to be explicit in checking. We usually utilize (and are encouraged to) the strict comparison operator (=== and !==). Its a habit that's extended to my PHP. (y)
 
Top Bottom