• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

xen:link guide

Jake Bunce

XenForo moderator
Staff member
#2
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.
 

Ryan Kent

Well-known member
#3
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?
 

Kier

XenForo Developer
Staff member
#4
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.
 

Ryan Kent

Well-known member
#5
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?
 

James

Well-known member
#6
Any parameter you might throw on the end of a URL. The link above basically outputs the following:
find-new/threads/?days=30
 

Jake Bunce

XenForo moderator
Staff member
#7
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
 

mauzao9

Well-known member
#8
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?
 

Jeremy

Well-known member
#11
HTML:
<xen:if is="$visitor['user_id'] != 0"><a href="{xen:link members, $visitor}">{xen:phrase view_your_profile}</a></xen:if>
That should do what you want.
 

Jeremy

Well-known member
#13
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)