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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Nested controls?


Go to End


3 Posts   2543 Views

Avatar
inCharge

Community Member, 102 Posts

7 July 2010 at 4:08am

Edited: 07/07/2010 4:09am

I can't get nested controls working. Am I doing something wrong?

In the example below, the datasets are static (to simplify the code). In real life, they are nested results of database queries.

When the two datasets are shown on their own, they work, but when they are nested inside each other, the inner control doesn't work. (See results at the end)

NestedControls.php

<?php

class NestedControls extends Page {

	static $db = array(	);

	public function Rows() {
		$Columns = array();
		$Columns[] = array( 'Content' => 'Row1' );
		$Columns[] = array( 'Content' => 'Row2' );
		$Columns[] = array( 'Content' => 'Row3' );
		return new DataObjectSet($Columns);
	}

	public function Columns() {
		$Columns = array();
		$Columns[] = array( 'Content' => 'Col1' );
		$Columns[] = array( 'Content' => 'Col2' );
		$Columns[] = array( 'Content' => 'Col3' );
		return new DataObjectSet($Columns);
	}

}

class NestedControls_Controller extends Page_Controller {

	public function init() {
		parent::init();
	}

}
?>

NestedControls.ss


<h1>Rows on their own</h1>
<% control Rows %>
	This is $Pos of $TotalItems: $Content<br/>
<% end_control %>

<h1>Columns on their own</h1>
<% control Columns %>
	This is $Pos of $TotalItems: $Content<br/>
<% end_control %>

<h1>Columns inside rows</h1>
<% control Rows %>
	This is $Pos of $TotalItems: $Content<br/>
	<% control Columns %>
		This is $Pos of $TotalItems: $Content<br/>
	<% end_control %>
<% end_control %>

<h1>Rows inside columns</h1>
<% control Columns %>
	This is $Pos of $TotalItems: $Content<br/>
	<% control Rows %>
		This is $Pos of $TotalItems: $Content<br/>
	<% end_control %>
<% end_control %>

Results

Rows on their own
This is 1 of 3: Row1
This is 2 of 3: Row2
This is 3 of 3: Row3

Columns on their own
This is 1 of 3: Col1
This is 2 of 3: Col2
This is 3 of 3: Col3

Columns inside rows
This is 1 of 3: Row1
This is 1 of :
This is 2 of 3: Row2
This is 1 of :
This is 3 of 3: Row3
This is 1 of :

Rows inside columns
This is 1 of 3: Col1
This is 1 of :
This is 2 of 3: Col2
This is 1 of :
This is 3 of 3: Col3
This is 1 of : 

Avatar
Hamish

Community Member, 712 Posts

7 July 2010 at 4:07pm

> <h1>Columns inside rows</h1> 
> <% control Rows %> 
>    This is $Pos of $TotalItems: $Content<br/> 
>    <% control Columns %> 

Fails because Columns is not a property or method of Rows.

> <h1>Rows inside columns</h1> 
> <% control Columns %> 
>    This is $Pos of $TotalItems: $Content<br/> 
>    <% control Rows %> 

Fails because Rows is not a property or method of Columns.

Rows and Columns are methods of NestedControls. I'm not sure what you're trying to achieve by nesting them like this. All you'll get is the same set of Rows (or Columns) repeated Columns.Count (or Rows.Count) times.

Avatar
inCharge

Community Member, 102 Posts

7 July 2010 at 8:42pm

Edited: 07/07/2010 8:45pm

Of course, I get it, Thanks Hamish.

The nested control needs the 'Top.' prefix to put it into the parent context.

> I'm not sure what you're trying to achieve by nesting them like this.
> All you'll get is the same set of Rows (or Columns) repeated Columns.Count (or Rows.Count) times.

True, but the Rows & Columns datasets are static to simplify the nested control example. In practice the datasets are query results and rows & columns contain different data.

The example again in full, with corrections:

NestedControls.php

<?php

class NestedControls extends Page {

	static $db = array(	);

	public function Rows() {
		$Columns = array();
		$Columns[] = array( 'Content' => 'Row1 data' );
		$Columns[] = array( 'Content' => 'Row2 data' );
		$Columns[] = array( 'Content' => 'Row3 data' );
		return new DataObjectSet($Columns);
	}

	public function Columns() {
		$Columns = array();
		$Columns[] = array( 'Content' => 'Col1 data' );
		$Columns[] = array( 'Content' => 'Col2 data' );
		$Columns[] = array( 'Content' => 'Col3 data' );
		return new DataObjectSet($Columns);
	}

}

class NestedControls_Controller extends Page_Controller {

	public function init() {
		parent::init();
	}

}
?>

NestedControls.ss


<h1>Rows on their own</h1>
<% control Rows %>
	Row $Pos of $TotalItems: $Content<br/>
<% end_control %>

<h1>Rows on their own</h1>
<% control Columns %>
	Column $Pos of $TotalItems: $Content<br/>
<% end_control %>

<h1>Columns inside rows</h1>
<% control Rows %>
	Row $Pos of $TotalItems: $Content<br/>
	<% control Top.Columns %>
		Column $Pos of $TotalItems: $Content<br/>
	<% end_control %>
<% end_control %>

<h1>Rows inside columns</h1>
<% control Columns %>
	Column $Pos of $TotalItems: $Content<br/>
	<% control Top.Rows %>
		Row $Pos of $TotalItems: $Content<br/>
	<% end_control %>
<% end_control %>

Results

Rows on their own
Row 1 of 3: Row1 data
Row 2 of 3: Row2 data
Row 3 of 3: Row3 data

Rows on their own
Column 1 of 3: Col1 data
Column 2 of 3: Col2 data
Column 3 of 3: Col3 data

Columns inside rows
Row 1 of 3: Row1 data
Column 1 of 3: Col1 data
Column 2 of 3: Col2 data
Column 3 of 3: Col3 data
Row 2 of 3: Row2 data
Column 1 of 3: Col1 data
Column 2 of 3: Col2 data
Column 3 of 3: Col3 data
Row 3 of 3: Row3 data
Column 1 of 3: Col1 data
Column 2 of 3: Col2 data
Column 3 of 3: Col3 data

Rows inside columns
Column 1 of 3: Col1 data
Row 1 of 3: Row1 data
Row 2 of 3: Row2 data
Row 3 of 3: Row3 data
Column 2 of 3: Col2 data
Row 1 of 3: Row1 data
Row 2 of 3: Row2 data
Row 3 of 3: Row3 data
Column 3 of 3: Col3 data
Row 1 of 3: Row1 data
Row 2 of 3: Row2 data
Row 3 of 3: Row3 data