Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Pass Variables inside a control

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

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

Page: 1
Go to End
Author Topic: 1789 Views
  • FlorianH
    Avatar
    Community Member
    33 Posts

    Pass Variables inside a control Link to this post

    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");
          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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Pass Variables inside a control Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Pass Variables inside a control Link to this post

    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.

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