3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 997 Views |
-
problem with execute function in tempalte

7 August 2009 at 3:24am
hi
i have a function:
function gwiazdki() {
$szare = 5 - $this->Trudnosc;
for($i=1; $i<=$this->Trudnosc; $i++) {
echo "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />";
}
for($i=1; $i<=$szare; $i++) {
echo "<img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />";
}
}$Trudnosc is a number: 0,1,2,3,4 or 5
and in template i put
$gwiazdkibut function runs on top of the site before <html> tag, why ?
<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" >
<head> -
Re: problem with execute function in tempalte

7 August 2009 at 7:16am
Rather than echoing it, you need to build it into an ArrayData object and return it. You can then loop through it in the template using <% control gwiazdki %>.
Google for how to build into ArrayData and if you get stuck I'll give you more of a hand
-
Re: problem with execute function in tempalte

7 August 2009 at 9:11am Last edited: 7 August 2009 9:27am
i tried like here http://silverstripe.org/all-other-modules/show/261496#post261496
function gwiazdki() {
$szare = 5 - $this->Trudnosc;
$data = new ArrayData();for($i=1; $i<=$this->Trudnosc; $i++) {
$data->push(new ArrayData(array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />")));
}
return $data;
}but i get an error
[Warning] Missing argument 1 for ArrayData::__construct()so i tried this:
function gwiazdki() {
$szare = 5 - $this->Trudnosc;for($i=1; $i<=$this->Trudnosc;; $i++) {
$a = array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />");
}
return new ArrayData($a);
}but it returns only one image
EDIT:
need to change
$data = new ArrayData(); to
$data = new DataObjectSet();works
-
Re: problem with execute function in tempalte

7 August 2009 at 9:27am Last edited: 7 August 2009 9:27am
Yeah, $a will be overwritten for each iteration. You were on the right track with the first one, but multiple ArrayData items should be loaded into a DataObjectSet. Try:
function gwiazdki() {
$szare = 5 - $this->Trudnosc;
$data = new DataObjectSet();for($i=1; $i<=$this->Trudnosc; $i++) {
$data->push(new ArrayData(array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />")));
}
return $data;
}
| 997 Views | ||
|
Page:
1
|
Go to Top |



