Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » $Link not being rendered

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1781 Views
  • J2-Paul
    Avatar
    Community Member
    51 Posts

    $Link not being rendered Link to this post

    I have a problem using the $Link page control.

    When I use the control outside a control loop all is fine it renders OK.

    However, in side table the $Link is not rendering the href in the loop shows as /goedit?id=$ID instead of /student-search/goedit?id=$id.

    Anyone see what I am doing wrong?

    Thanks

    --------------------------------------------------

          <% if StudentSearchResults %>
          
             <p>Total: $StudentSearchResults.Count
             $Link
             </p>
       
             <table width="99%">
                <thead>
                   <tr>
                      <th></th>
                      <th>Student</th>
                      <th>ID</th>
                   </tr>
                </thead>
                <tbody>
                   <% control StudentSearchResults %>
                      <tr>
                         <td>$Link</td>
                         <td><a href="$Link/goedit?id=$ID">Edit</a></td>
                         <td>$FirstName $Lastname</td>
                         <td>$ID</td>
                      </tr>
                   <% end_control %>
                </tbody>
             </table>

          <% end_if %>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: $Link not being rendered Link to this post

    Hello again.

    Because you are in the control loop for the student, SS thinks you are looking for the $Link property or method of the student. But you want the link of the page.

    There are two options here, in your student class you can add a Link() method that will return the link:

    function Link() {
    return $this->CurrentPage()->Link() . 'goedit?id=' . $this->ID;
    }


    This will allow you to use just $Link in the control loop.

    OR

    you can get the current pages link in the template by proceeding the $Link with $Top like so:

    ...
    <td><a href="$Top.Link/goedit?id=$ID">Edit</a></td>

    You may have to surround the $Top.Link with curly brackets too: {$Top.Link}

    I recommend the first method

  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: $Link not being rendered Link to this post

    Thanks again. Dan, I have it working, but only using your least favourite option I am afraid.

    Firstly, when creating the method name "link()" the page never rendered, it just spinnered indefinitely. I then changed the method name to Pagelink() and the page rendered as per normal. I guess the word "link" must be a resevered/protected name for a method.?

    Furthermore, the method returns an empty string. Any ideans?

    ...
          function Pagelink() {
          
             return $this->CurrentPage()->Link() . 'goedit?id=' . $this->ID;
          }

    ...
    <% control StudentSearchResults %>
                 <tr>
                  <td><a href="{$Top.Link}goedit?id=$ID">Edit</a></td> <<< this one worked.
                   <td><a href="{$Pagelink}goedit?id=$ID">Edit</a></td> <<< this one did not.
                   <td>$FirstName $Lastname</td>
                   <td>$ID</td>
                </tr>
        <% end_control %>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: $Link not being rendered Link to this post

    That function looks fine. Make sure that it is a method of the object that you are controlling.

    ie: Pagelink() has to be a function defined in the Student object (if the student object is being returned in the StudentSearchResults)

    Also, you would only need to use $Pagelink in the template not {$Pagelink}editid=$ID

    Tbh, if you can't get $Pagelink working, but $Top.Link/editid=$ID does, then use that.

  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: $Link not being rendered Link to this post

    Dan. A penny has just dropped! I had the method in the wrong controller. Moved it to the student object and hey presto all is good.

    Solving this one has also solved a few other queries too!

    Thanks again Dan. !

    1781 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.