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.


Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

generate tables in template


7 Posts   2499 Views


14 November 2007 at 8:11pm Community Member, 2 Posts

Hello everybody,

I'm still quite new to Silverstripe - But am quite enthusiastic about it.

I have one question. Imagine i have something similar to the newssection as created in the tutorial, but now I want to show it in a table.
So instead of all items being iterated, and show below each other, i want to create table cells. And that's easy, but i can't find how to automatically insert a new table row after let's say each 3 cells.

Below I have the control code from the template. And this is working, except I need to generate every 3 <td></td> and additional </tr><tr>.

<% control Children %>
         <p>$Pos - $Title</p>
         <embed src="$URL" type="application/x-shockwave-flash" width="200" height="173" allowFullScreen="true"></embed><br />
         <% if NewRow %>
             <p>tsja - en nu is newrow true</p>
          <% else %>
             <p>tsja - en nu is newrow false</p>
         <% end_if %>

   <% end_control %>

hope someone can give me some help


25 November 2007 at 12:17pm (Last edited: 25 November 2007 12:18pm), Forum Moderator, 921 Posts

Can you not move the <tr> and </tr> elements inside the <% control Children %> block?

Oh, but then each child would have a <tr>... Hmm, you'll probably need a custom method to generate this table for you.



25 November 2007 at 4:15pm Forum Moderator, 5511 Posts

any reason why you need to use a table? Couldn't you still use a list, Give the unordered list a width, Give each list item a width (1/3 of the ul) , float the list items. Hopefully then you will have 3 columns and no need for a custom method to generate the table which could be interesting to achieve.


25 November 2007 at 10:53pm Forum Moderator, 921 Posts

Quite true. Tables are a pain to style as well, especially adding borders to <tr> which is inconsistent across browsers.



26 November 2007 at 10:38am Community Member, 2 Posts

thanks for the input
I can try to do it with the ul
But i'm also really curious how i can do something like that. I was thinking that i can maybe make a function in the controller to give me a row of 3 every time - but even then it's hard to get it working good - including pagination.

For now i've solved it by just having one column -but for another part i will need to be able to do this.
Isnt there a way i can just call a procedure - that i would put in the controller - and that would create the html in php - and then return the whole table (that i'd create in the php).
The next question then would be, how can i do pagination.

a second question - i've implemented pagination - but how can i jsut chagne the number of items on a page?

thanks very much
Bas Smit


26 November 2007 at 9:42pm Forum Moderator, 5511 Posts

you can change the amount of items per page with the 5th argument for the DataObject::get call so for pagination if you read this page - you can see the DataObject request is

$doSet = DataObject::get(
$callerClass = "ArticlePage",
$filter = "`ParentID` = '".$this->ID."'",
$sort = "",
$join = "",
$limit = "{$_GET['start']},2"

The last one is the value you will need to edit.


27 November 2007 at 12:12pm (Last edited: 27 November 2007 12:13pm), Forum Moderator, 921 Posts

Instead of using <% control Children %> you could implement your own method like <% control ChildrenWithLimit %> which may look something like this in your Page.php file:

Note: This goes in the Page_Controller class:

function ChildrenWithLimit() {
$start = $_GET['start'] ? (int)$_GET['start'] : 0;
$limit = "$start,5";
if($childPages = DataObject::get('Page', "ParentID = $this->ID", '', '', $limit)) {
return $childPages;

Just change the 5 to how many results you want per page.

Then, you're able to use the pagination abilities as outlined at the bottom of this wiki page:

An actual example of how pagination is implemented is the tutorial 4 - search results: [url][/url]. If you look at the template in the tutorial, it has the necessary template controls.

Hope this helps,