Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Parse Error in Template

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

Page: 1
Go to End
Author Topic: 1429 Views
  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Parse Error in Template Link to this post

    Having followed all of the tutorials, I decided to modify the ArticleHolder type so that it could have ArticleHolder children, which in turn would have ArticlePage children. I changed the $allowed_children setting without a problem, however I get a parse error with my modified ArticleHolder.ss, which checks if the children are ArticleHolders or Article Pages and only uses the required fields.

    This is my /themes/tutorial/templates/Layout/ArticleHolder.ss:

    <div id="Content" class="typography">      
    $Content
    <ul id="NewsList">
          <% if this->Child()->ClassName = ArticlePage %>
             <% control Children %>
          <li class="newsDateTitle"><a href="$Link" title="Read more on $Title">$Title</a></li>
          <li class="newsDateTitle">$Date.Nice, $Qualification</li>
          <li class="newsSummary">$Description <a href="$Link" title="Read more on $Title">Read more &gt;&gt;</a></li>
             <% end_control %>
          <% else %>
             <% control Children %>
             <li class="newsDateTitle"><a href="$Link" title="Read more about $Title">$Title</a></li>
             <li class="newsDateTitle">$Qualification</li>
             <li class="newsSummary">$Description <a href="$Link" title="Read more about $Title">Read more &gt;&gt;</a></li>
             <% end_control %>
          <% end_if %>
    </ul>
    </div>

    and this is the error I get:

    Parse error: syntax error, unexpected '}' in /tmp/silverstripe-cache-home-irevise-public_html/.cache.home.irevise.public_html.themes.tutorial.templates.Layout.ArticleHolder.ss on line 58

    I've searched through ArticleHolder.ss, and can't find any out-of-place braces. Can anybody tell me what's going wrong here?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Parse Error in Template Link to this post

    Hi,

    The template errors rarely point to the exact issue in my expiernce... what I think is wrong above is...

    <% if this->Child()->ClassName = ArticlePage %>

    i)you never need "this" in templates you are in the controller "context" by default each <% control %> shifts the context and things like $Top can be used to use the controller context when still in a control

    ii)to call a method / object use the '.' not the '->'

    I'd suggest putting a function like this in the controller...

    function IsChildClassName($str)
    {
       return ($this->Child()->ClassName == $str);
    }

    then replace the template line with....

    <% if IsChildClassName(ArticlePage) %>

    (note all untested)

    there might be a "Child.Classname = " possible (comething like Top.Classname would work I am sure) and if it is that would make the controller function redundant. It is good practice I believe to put anything complicated in the controller and keep the template very simple

    Barry

  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Re: Parse Error in Template Link to this post

    Thank you for the reply.

    I've changed the code, and it gets rid of the plain-text parsing error, however SilverStripe now shows its 'Website Error: The website server has not been able to respond to your request.' message. I've done some testing, and this is definitely down to the ArticleHolder.ss if-else construct. Can anybody help me with this error?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Parse Error in Template Link to this post

    That sounds like a PHP error, do you have the following in you mysite/_config.php?

    Director::set_environment_type('dev');

    check out...
    http://doc.silverstripe.org/common-problems

    p.s. I did mention the code was untested!

  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Re: Parse Error in Template Link to this post

    It seems that the problem is with the Child() function - it doesn't exist. I've been trying to fiddle with ChildrenOf() to make it work, but I can't. Can anybody see what could be fixed in that line to make it work?

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Parse Error in Template Link to this post

    You might want to add a check for Child in the IsChildClassName method:

    function IsChildClassName($str){
    if($this->Child()){
    return ($this->Child()->ClassName == $str);
    }
    }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Parse Error in Template Link to this post

    Alright, from looking at this it seems that there are no children, there are only parents!

    As in each page records it's parent, so you can find a pages children by using...

    $dosChildPages = DataObject::get('Page','ParentID='.$iPageID);

    So now you have to look at your logic again and decide whether it is contains at least one page that is of classname=xyz or something else. Looking back on you original code it was obvious it needed changing because it always assumed a return (by calling ->Classname without knowing if it even had one object/child) and also assumed only one object would be return (pages can have many children).

    I'd assumed your php was ok, but you were learning templates, sorry.

    btw i'd advise using netbeans or similar to provide code completion, then you'd aways know which method (and get it typed in) with the minimum of effort

  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Re: Parse Error in Template Link to this post

    Sorry for the lack of reply in a while.

    It seems that coding the way I was originally intending is throwing up too many complications, so I've re-worked my logic to avoid this problem. Thank you again to everybody who contributed in trying to fix my issue.

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