Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

access $Link inside a <% control .. %>


Reply

3 Posts   1450 Views

Avatar
escaped

23 November 2009 at 5:53am Community Member, 6 Posts

hey,

i have the following controller:

class ImagePage_Controller extends Page_Controller {
   function download() {      
      $id = Director::urlParam('ID');
      
      if ($id) {       
         //get a File object or subclass of (e.g. Image)
         $image = DataObject::get_by_id("Image",$id);
         $path = $image->getFullPath();
         $name = basename($path);
         $response = new HttpResponse();
         $response->setBody(file_get_contents($path));
         $response->addHeader("Content-disposition","attachment; filename=" . $name);
         
         return $response;   
      } else
         return false;
   }
}

and the following template:

<% control Images %>
   <div class="images">
      <% control Attachment %>$CroppedImage(100,100)<% end_control %>
      <p><a href="$Attachment.GetURL" title="download">download</a></p>
      <div class="clearleft"></div>
   </div>   
<% end_control %>


Images is an DataObject, which contains an Image in field 'attachment'.

What i am trying to do is:
Instead of linking to the direct file, i want to call the download-Function of the controller with the id if the image. Something like:

<p><a href="$Link(download)/$Attachment.ID" title="download">download</a></p>

But i cannot access the '$Link' inside a control. Is there any way to achieve this?
any ideas or solutions?

thanks

escaped

Avatar
Pigeon

23 November 2009 at 8:24am Community Member, 243 Posts

Hi escaped,

To be honest, i dont completely follow what you want to do. But if you want to access a variable or method from the page you are in whilst in a control, you can use $Top.

So $Top.Link should work for you.

Avatar
escaped

23 November 2009 at 9:55am Community Member, 6 Posts

thanks... That was, what i was searching for :)