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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Call function from template


Reply

747 Views

Avatar
Mackodlak

27 May 2011 at 2:34am (Last edited: 27 May 2011 2:39am), Community Member, 95 Posts

Hello,
I have a need to call a function from template (html) with 1 parameter.

Here is the thing: I have a LOTDpage page type (LOTDPage.php: LOTDPage extends Page; LOTDPage_Controller extends Page_Controller), it is used for displaying LOTDs (lotd is DataObject - LOTD.php: LOTD extends DataObject).

The function for catching all LOTDs goes like this (it is in LOTDPage.php):

function getLOTDs () {
return DataObject::get('LOTD',"","Created DESC","","");
}

and in template I am calling it by (LOTDPage.ss):

<% control getLOTDs %>   
<li>
<a href="$LOTD">$LName</a>
</br>
<% if Top.canDelLOTD %>
<a href="LOTD/delLOTD/$ID">Brisanje</a>
<% end_if %>
</li>
<% end_control %>

OK. now the problem. In this case LOTDPage.php also contains function delLOTD:

   function delLOTD($request) {
      $param = $request->allParams();
      $LOTDID=(int)$param['ID'];
      DB::query("DELETE FROM LOTD WHERE ID = '$LOTDID'");
      Director::redirectBack();
   }

Clicking on 'Brisanje' on my page for some reason results in deleting the wanted LOTD from MySQL database, but I don't understand how and why... (I want it to do just that, but I don't understand how it works)
On similar example (I won't type it all over again, it's the same thing but instead LOTDPage.php is RSSPage.php, LOTD.php is MyRSS.php and delRSS($request) instead delLOTD($request) - <a href="/MyRSS/delRSS/$ID">) it doesn't work.
What I need is to be able to call the delete function for any DataObject from <% control %> block by passing the $ID of the current DataObject so I can delete the right one.
How is it done? What is the logic behind <a href="/LOTD/delLOTD/$ID"> Why does it work? Why <a href="/MyRSS/delRSS/$ID> doesn't work? Is there a better way to do it? I need it to be done either from <a href="" onclick=""> or from <BUTTON... onclick=""> or sth simmilar. The idea is do [function($ID)] on [DataObject['ID']] when click (link or button or whatever) from page on my site.

Hope you guys understand what I need and you can help me!
Ty all in advance!