Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Call function from template

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

Page: 1
Go to End
Author Topic: 740 Views
  • Mackodlak
    Community Member
    95 Posts

    Call function from template Link to this post

    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 %>   
    <a href="$LOTD">$LName</a>
    <% if Top.canDelLOTD %>
    <a href="LOTD/delLOTD/$ID">Brisanje</a>
    <% end_if %>
    <% end_control %>

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

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

    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!

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.