Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Question about the use of a function in a control block

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: 998 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Question about the use of a function in a control block Link to this post

    Suppose I have the following function (in Page.php):

       function showMensSweaters() {
          $whereStatement = "MenCat = 1 AND SweaterCat = 1";
          return DataObject::get("Page", $whereStatement);
       }

    When I call it in a template with a control block, like so (simplified html for readability):

    <% control showMensSweaters %>

    <a href="$Link" title="View style {$MenuTitle.XML}">$Photo.SetWidth(108)</a>

    <% control Styles %>
    <p>$StyleNumber</p>
    <% end_control %>

    <% end_control %>

    it all works. The pages for which the booleans MenCat and SweaterCat are true, are iterated over and rendered. Also, the "Styles" block is included for those pages where a Style is "attached" with a many-many relationship. All is good.

    But if I change the whole thing in such a way, that one of the variables for the function is not a boolean but, say, of the text type, like so (and of course change all the associated code accordingly, in how the variables are set in the CMS etc.):

       function showMensSweaters() {
          $whereStatement = "MenCat = 1 AND SweaterCat = bigsweater";
          return DataObject::get("Page", $whereStatement);
       }

    the thing blows up: the template doesn't render at all. What do I have to do to make that kind of thing work?

    Thanks in advance!

    (edit: added correct 'code' tags)

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Question about the use of a function in a control block Link to this post

    The string needs quotes around it, otherwise the database does a field comparison instead of a string comparison.

    function showMensSweaters() {
    $whereStatement = "MenCat = 1 AND SweaterCat = 'bigsweater'";
    return DataObject::get("Page", $whereStatement);
    }

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