17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 940 Views |
-
Question about the use of a function in a control block

5 September 2008 at 6:14am Last edited: 5 September 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)
-
Re: Question about the use of a function in a control block

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);
}
| 940 Views | ||
|
Page:
1
|
Go to Top |

