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 /

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

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


Go to End
Reply


3 Posts   1702 Views

Avatar
escaped

Community Member, 6 Posts

23 November 2009 at 5:53am

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
dhensby

Community Member, 247 Posts

23 November 2009 at 8:24am

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

Community Member, 6 Posts

23 November 2009 at 9:55am

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