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

xen:link guide

Discussion in 'XenForo Questions and Support' started by Ryan Kent, May 7, 2011.

  1. Ryan Kent

    Ryan Kent Well-Known Member

    Is there any basic tutorial or guide available describing what xen:links are and how to use them?
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    It's best to look at examples in the default templates.

    The basic form is this:

    <a href="{xen:link 'ROUTE'}">TEXT</a>
    For example, this will link to the user upgrade page:

    <a href="{xen:link account/upgrades}">{xen:phrase account_upgrades}</a>
    The corresponding URL would be:


    It takes the route and builds it into a hyperlink.

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

    <a href="{xen:link 'threads', $thread}">Link to thread</a>
    The handler extracts the appropriate information from the thread record to build the link.
  3. Ryan Kent

    Ryan Kent Well-Known Member

    Very helpful as always, thanks Jake.

    So if I wanted to link to this thread years ago I would do it as:
    <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:
    [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?
    maitandat1507 likes this.
  4. Kier

    Kier XenForo Developer Staff Member

    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.
  5. Ryan Kent

    Ryan Kent Well-Known Member

    Thx Kier.

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

    <li><a href="{xen:link 'find-new/threads', '', 'days=30'}">Latest Threads</a></li>
    What parameters does xen:link accept?
  6. James

    James Well-Known Member

    Any parameter you might throw on the end of a URL. The link above basically outputs the following:
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    See this post by Kier:

    Mythotical likes this.
  8. mauzao9

    mauzao9 Well-Known Member

    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?
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    {xen:link members, $visitor}
    $visitor must be available in the template and reference the user visiting the page.
    mauzao9 likes this.
  10. mauzao9

    mauzao9 Well-Known Member

    Thanks, that will do, must only find the code to not show to guests and it's done. :)
  11. Jeremy

    Jeremy XenForo Moderator Staff Member

    <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.
    mauzao9 likes this.
  12. James

    James Well-Known Member

    I think checking for the presence of user_id is enough without the comparison operator.
  13. Jeremy

    Jeremy XenForo Moderator Staff Member

    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)
    James likes this.
  14. Tracy Perry

    Tracy Perry Well-Known Member

    And not a bad habit to have. Better to be to cautious than not cautious enough. :sneaky:

Share This Page