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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Passing an ID value to a function


Go to End


546 Views

Avatar
hano

Community Member, 1 Post

27 April 2013 at 2:09am

I want to achieve a seemingly simple task, however I have been searching the web and the forums here to no avail for more than a week
My problem is this

I have enabled search form in a silverstripe website and have managed to successfully implement the search of dataobjects on the site.

However when the DataObjects(Products), are displayed in the search results I want to be able to link them to the category pages that they belong to. To achieve this I wrote a simple function in the page controller, that will return the category page based on the ID provided.

public function getCategoryPages($theID) {
return SiteTree::get()->filter('ID',$theID);
}

and I wanted to be able to do this in the template

<% loop $Results %>

<% if CategoryID %>

<% loop $Top.getCategoryPages($CategoryID) %>
<a href="$Link">Link</a>
<% end_loop %>
<% end_if %>
<% end_loop %>

The problem is no matter what I do I am not able to pass the correct $CategoryID value to the function. I have tried, almost everything but I am not able to do so, please someone point me in the right direction, it was my understanding that this is possible to do in SilverStripe 3.0 which is the version I am using. I am I trying to do something that is not possible? If so how can I get around this problem? Any help would be greatly appreciated (I have pulled all of my hair out trying to figure this out :( )