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

How do I get a Thread URL in a Template if I only have the Thread ID?

Discussion in 'XenForo Questions and Support' started by robdog, Mar 24, 2011.

  1. robdog

    robdog Well-Known Member

    Thanks.

    * I did search this time and checked the FAQ. Honest! :)
     
  2. James

    James Well-Known Member

  3. Kier

    Kier XenForo Developer Staff Member

    HTML:
    <a href="{xen:link 'threads', {xen:array 'thread_id={$threadId}'}}">Link to thread</a>
    It won't contain the title, but it will work and it's future proof.
     
  4. robdog

    robdog Well-Known Member

    Thanks KIER!

    Exactly what I was looking for.
     
  5. ragtek

    ragtek Guest

    i've tried:

    {xen:link 'foobar/baz', {xen:array 'post_id={$post.post_id}'}}
    // $post is a array containtaing post_id & title
    and the generated link is only foobar/baz, but i need foobar/baz?post_id=240

    do i need to set anything in the route buildlink?? (that's what i don't want to do:/
     
  6. James

    James Well-Known Member

    have you tried:
    {xen:link 'foobar/baz', '', 'post_id={$post.post_id}'}
     
  7. ragtek

    ragtek Guest

    yes
    but i ended using a own buildlink method:/
     
  8. CyberAP

    CyberAP Well-Known Member

    How to make it work with pages? For ex: {xen:link 'page', 10}
     
  9. ragtek

    ragtek Guest

    <a href="{xen:link 'pages', {xen:array 'node_id=10'}}">Link to page</a>
     
  10. CyberAP

    CyberAP Well-Known Member

    Tried. Doesn't work for me.
     
  11. ragtek

    ragtek Guest

    ups
    sorry, hadn't checked it before posting:D


    it seems that it's not possible to do it with the id:(

    you'll need the node name
    node_name = xxx (node name) works for me:)

    <a href="{xen:link 'pages', {xen:array 'node_name=test2dddaads'}}">Link to page</a>
     
  12. Brogan

    Brogan XenForo Moderator Staff Member

    Far easier just to do this, surely?

    <a href="{xen:link 'pages/page-title/'}">Page link</a>

    Unless you're doing it programmatically of course.
     
  13. CyberAP

    CyberAP Well-Known Member

    I wanted to use xen:link in order to have my links working even without SEO urls, so that's why I used the ID.
     
  14. account8226

    account8226 Guest

    If I have my own link (custom add-on), how could I add something like /myroute/subroute/ID/do-that using the {xenlink} syntax ?
     
  15. tenants

    tenants Well-Known Member

    I'm looking back at this old post, and there is some information missing... And it seemed that many people here were asking the same question in a round about way.

    If you have your own route prefix for a given add-on ... for instance, lets say you have a blogs add-on, and you want to create a <xen:link in the template such as:

    <xen:link blogs/edit $thisBlog>

    and expect that to return

    myforum.com/blogs/my-first-blog.1/edit


    In order to do this, you need to create "public function buildLink" in your route prefix

    Example:

    PHP:
    <?php
    class SomeBlogPlugin_Route_Prefix_Blog implements XenForo_Route_Interface
    {
     
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            
    $action $router->resolveActionWithIntegerParam($routePath$request'node_id');
            return 
    $router->getRouteMatch('SomeBlogPlugin_ControllerPublic_Blog'$action'blog');
        }
     
     
        public function 
    buildLink($originalPrefix$outputPrefix$action$extension$data, array &$extraParams)
        {
            return 
    XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix$action$extension$data'node_id''title');
        }
     
    }


    So now in your template, as long as $thisBlog contains $thisBlog['node_id'] and $thisBlog['title']

    <xen:link blogs/edit $thisBlog> will resolve as myforum.com/blogs/[title].[node_id]/edit

    or from Kiers example, now that you have the buildLink function for your router you can simply use:

    <xen:link blogs/edit {xen:array 'node_id={$node_id}', 'title={$title}'}>




    In your case , since you only want to resolve the id (and not the title too), your public function buildLink for the "something" route prefix would contain

    Code:
        public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
        {
            return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'id');
        }
    So now, when you use <xen:link something/do-that $thisThing> in your template, this will resolve as forum/something/ID/do-that (as long as $thisThing contains id)
     
    Liam W and account8226 like this.
  16. Liam W

    Liam W Well-Known Member


    Excellent, thanks!

    Now, how do I get the data back from the URL?
     
  17. tenants

    tenants Well-Known Member

    In your template, you would have something like this:
    Code:
    <xen:link blogs/edit $blog>
    which would resolve as this: myforum.com/blogs/my-first-blog.1/edit (as long as the array $blog contains 'node_id' and 'title')

    In your route prefix, you would define
    Code:
    $action = $router->resolveActionWithIntegerParam($routePath, $request, 'node_id');
    In your controllerPublic you would have an action "actionEdit" and this would contain
    Code:
    $nodeId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
    
    and then you would do a look up to get the data needed for that node_id
    Code:
    $thisBlog =  $blogModel->getBlogById($nodeId);
     

Share This Page