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.

Archive /

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

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

Pass Variables inside a control


Reply


3 Posts   1821 Views

Avatar
FlorianH

Community Member, 33 Posts

16 July 2008 at 7:51pm

Hello Silverstripe Community,

as I read in the [URL=http://doc.silverstripe.com/doku.php?id=templates&s=controller%20parameter]Silverstripe Documentation[/URL] 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");
      if($quer)
         return $quer->Count();
      else
      {
         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\.cacheC..wamp.www.silverstripe-v2.2.2.tutorial.templates.Layout.HomePage.ss 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.

Florian

Avatar
Willr

Forum Moderator, 5513 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.

Avatar
Sean

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.