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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

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

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

generate tables in template


Go to End


7 Posts   2844 Views

Avatar
Bas

Community Member, 2 Posts

14 November 2007 at 8:11pm

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>.

<table>
<tr>
<% control Children %>
<td>
<p>$Pos - $Title</p>
<p>$Iterator</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 %>

</td>

<% end_control %>
</tr>
</table>

hope someone can give me some help
Thanks
Bas

Avatar
Sean

Forum Moderator, 922 Posts

25 November 2007 at 12:17pm

Edited: 25/11/2007 12:18pm

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.

Sean

Avatar
Willr

Forum Moderator, 5523 Posts

25 November 2007 at 4:15pm

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.

Avatar
Sean

Forum Moderator, 922 Posts

25 November 2007 at 10:53pm

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

Sean

Avatar
Bas

Community Member, 2 Posts

26 November 2007 at 10:38am

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

Avatar
Willr

Forum Moderator, 5523 Posts

26 November 2007 at 9:42pm

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 - http://doc.silverstripe.com/doku.php?id=private:recipes:pagination 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.

Avatar
Sean

Forum Moderator, 922 Posts

27 November 2007 at 12:12pm

Edited: 27/11/2007 12:13pm

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:
http://doc.silverstripe.com/doku.php?id=built-in-page-controls.

An actual example of how pagination is implemented is the tutorial 4 - search results: http://doc.silverstripe.com/doku.php?id=tutorial:4-site-search. If you look at the Page_results.ss template in the tutorial, it has the necessary template controls.

Hope this helps,

Cheers,
Sean