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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

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

Pass Variables inside a control

Go to End

3 Posts   2183 Views


Community Member, 33 Posts

16 July 2008 at 7:51pm

Hello Silverstripe Community,

as I read in the Silverstripe Documentation it's impossible to pass Variables inside a control as a parameter. Unfortunately there is no solution written in this doc, so I struggle with this problem for some time now. Here is some Code to explain the problem in detail:

My control function:

function CountComments($newsid)
		$quer = DataObject::get("PageComment", "ParentID = $newsid");
			return $quer->Count();
			return 0;

So I'm trying to use this function in a template while passing the $ID of another control as a parameter:

<% control LatestNews %>
			<li class="newsDateTitle"><a href="$Link" title="Mehr von &quot;{$Title}&quot;">$Title, $TotalItems</a></li>
			<li class="newsDateTitle">Geschrieben am $Date.Nice um $Time.Nice von $Author</li>
			<li class="newsSummary">$Content <a href="$Link" title="Mehr von &quot;{$Title}&quot;">$Top.CountComments($ID) Kommentar(e) &gt;&gt;</a></li>
		<% end_control %>

"$Top.CountComments($ID)" is the call I struggle with, I receive the following error message:

Parse error: syntax error, unexpected T_STRING, expecting ')' in D:\WINDOWS\Temp\silverstripe-cacheC--wamp-www-silverstripe-v2.2.2\ on line 63

When I pass a number instead of the variable the code works fine. So how can I handle this?Thanks in advance.



Forum Moderator, 5523 Posts

16 July 2008 at 8:37pm

If you are just trying to show the number of comments on the page you can just do $Comments.Count in the template. Im pretty sure that will output the amount of comments that page has.


Forum Moderator, 922 Posts

16 July 2008 at 9:24pm

No, it's not possible to do that in the SS templating language.

Typically the templates are used for just dumb tasks like outputting variables and looping through iterative data from the model/controller, so if you're doing more complex actions, it's better (and most of the time easier) to just put the logic on the model and output it directly in the template.