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

Question about the use of a function in a control block


Reply


2 Posts   1058 Views

Avatar
SilverRay

Community Member, 167 Posts

5 September 2008 at 6:14am

Edited: 05/09/2008 6:16am

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)

Avatar
simon_w

Forum Moderator, 474 Posts

5 September 2008 at 7:41am

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