Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » Nested controls?

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

Page: 1
Go to End
Author Topic: 1826 Views
  • inCharge
    Avatar
    Community Member
    102 Posts

    Nested controls? Link to this post

    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 :

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Nested controls? Link to this post

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

  • inCharge
    Avatar
    Community Member
    102 Posts

    Re: Nested controls? Link to this post

    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

    1826 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.