Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Multiple function calls from links

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

Page: 1
Go to End
Author Topic: 595 Views
  • LisaB
    Avatar
    Community Member
    28 Posts

    Multiple function calls from links Link to this post

    Hi all, In my template, ServiceProductHolder.ss, I have a control loop listing multiple ServiceProducts and within the control block I have:

    <p><a href="$saveToMember">Add to My Programme</a></p>

    I want saveToMember to run when the link is clicked, but it is obviously running for every ServiceProduct listed when the page loads. This must be a problem with my template syntax - how can I stop this and run it only when the link is clicked?

    I have something similar working elsewhere using something like:

    <a href="{$Top.Link}functionName">Do function</a>

    but because this function is on the page controller it's slightly different.

    Any help much appreciated!

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Multiple function calls from links Link to this post

    Another thought on this. It seems it might not be possible to call my function that is on the data object from a link click because you can't call it via a URL as you could if it were on the page controller. If you put it in the template like I have above it will always run on page load when the template is parsed.

    So, I started to look at doing this with a form and now I have a new problem with where to put the code. Because the easiest way to create the relation is in the ServiceProduct data object like this:

    $member->ServiceProducts()->add($this);

    And because the form needs to be displayed within the control block, I put the form in ServiceProduct.php. However I get method link() doesn't exist on ServiceProduct when trying to visit the holder page.

    However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff.

    I'm thinking of going back to a text link and ajax but will have to learn about it first!

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Multiple function calls from links Link to this post

    For posterity, in case it's ever useful to anyone:

    "However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff."

    I was missing something easy. I put everything in the page controller rather than the data object, then from within my loop on the page template where I was controlling the data objects I used <a href="{$Top.Link}functionname/$ID">link</a>. The function gets that DO from the url parameter in order to create or destroy the relationship between it and the logged in member.

    Thanks to r3v3rb and other on IRC for pointing me in the right direction.

    595 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.