Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Children: Controllers and templates

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

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

Page: 1
Go to End
Author Topic: 1746 Views
  • KatB
    Avatar
    Community Member
    94 Posts

    Children: Controllers and templates Link to this post

    In Tutorial 2, an ArticleHolder has ArticlePage children.

    Perhaps, sometimes, ArticlePages can be written on the same day. And maybe you only want the day it is written on to be displayed once, like a blog. Blogs can have the day of posting as a header followed by all the blog posts posted on that day.

    How and where can ArticleHolder access the data of the Children to determine whether or not it should print the day.

    So if $day is the same as $blog.prev.day, don't print, else print.

    Is this done in the Controller or is this done in the template? What sort of code can be used to test the data held by the Children?

    Or is it not possible to query Children?

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Children: Controllers and templates Link to this post

    Wow, still no response.

    I honestly haven't figured this out. It would be good if the Child could pass back its status to the controller, who can keep tab on what has happened, however, it turns out that you can't send variables from the template to the controller.

    Any help? Please?

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Children: Controllers and templates Link to this post

    There is clearly some communication going when you have $TotalItems and $Pos.

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Children: Controllers and templates Link to this post

    Here's a question: can a function keep count of how many times it has been called, by the template?

    *sigh* Doesn't help. Seems that in a <% control Children %> block, only the children's methods are called, never the methods of the page that has the children.

    What does <% control Parent %> refer to?

    For example, for Article Page, does it refer to Page or does it refer to ArticleHolder?

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Children: Controllers and templates Link to this post

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Children: Controllers and templates Link to this post

    <% Parent %> refers to ArticleHolder yay!

    AND I have managed to count the number of times the function in ArticleHolder is called with code looking something like:

    NB. please excuse its dodginess - I am using it to test and learn with.

    ArticleHolder_Controller {
    function Asdf(){
    static $a = 0;
    $a++;
    return $a;
    }

    template:
    $Parent.Asdf

    YAY!

  • KatB
    Avatar
    Community Member
    94 Posts

    Re: Children: Controllers and templates Link to this post

    My dodgy code to get my original question to work.

    NB. This is probably wrong on many levels, but it's a start! And if you can improve it, go ahead And it probably breaks with pagination.

    class ArticleHolder extends Page {
    ...
    function Articles(){
    $data = $this->Children();
    $data->sort('Date');
    return $data;
    }

    function getDates(){
    static $dates;

    if (!isset($dates)) {

    $data = $this->Children();
    $data->sort('Date');

    $dates = array();

    for ($i = 0; $i < $data->TotalItems(); $i++){
    $date = $data->Current()->getField('Date');
    $dates[$i] = strtotime($date);
    $data->next();
    } // end for loop
    } // end !isset($dates)

    return $dates;

    } // end function getDates()

    function printDate(){
    static $a = 0;
    $dateArray = $this->getDates();

    $print = true;

    if ($a > 0) {
    if ($dateArray[$a] == $dateArray[$a-1])
    $print = false;
    }

    $a++;
    return $print;
    }

    function endDate(){
    static $a = 0;
    $dateArray = $this->getDates();

    $print = true;

    if ($a < count($dateArray)) {
    if ($dateArray[$a] == $dateArray[$a+1])
    $print = false;
    }

    $a++;
    return $print;
    }
    }

    ArticleHolder.ss
    <div id="Content" class="typography">      
    $Content
    <% control Articles %>

    <% if Parent.printDate %><div class="date"><h2 class="newsDateTitle">$Date.Nice</h2><% end_if %>
    <div class="newsArticle">
    <h3 class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h3>
    <p class="newsSummary">$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>

    <% if Parent.endDate %></div><!-- end of div.newsArticle --><% end_if %>
    </div>
    <% end_control %>
    </ul>
    </div>

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