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.

Template Questions /

problem with execute function in tempalte


Go to End
Reply


5 Posts   1272 Views

Avatar
snaip

Community Member, 180 Posts

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
$gwiazdki

but 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" xml:lang="en" >
<head>

Avatar
joshy

Community Member, 57 Posts

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 :)

Avatar
snaip

Community Member, 180 Posts

7 August 2009 at 9:11am

Edited: 07/08/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 :)

Avatar
Hamish

Community Member, 712 Posts

7 August 2009 at 9:27am

Edited: 07/08/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;        
}

Avatar
snaip

Community Member, 180 Posts

7 August 2009 at 9:27am

yea it works now :)