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.

Archive /

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

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

Children: Controllers and templates


Reply


7 Posts   1806 Views

Avatar
KatB

Community Member, 94 Posts

22 January 2008 at 12:56pm

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?

Avatar
KatB

Community Member, 94 Posts

23 January 2008 at 4:12pm

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?

Avatar
KatB

Community Member, 94 Posts

23 January 2008 at 4:38pm

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

Avatar
KatB

Community Member, 94 Posts

23 January 2008 at 5:34pm

Edited: 23/01/2008 6:50pm

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?

Avatar
SilverRay

Community Member, 167 Posts

23 January 2008 at 7:22pm

Edited: 23/01/2008 7:23pm

http://doc.silverstripe.com/doku.php?id=built-in-page-controls

maybe this will help you?

Avatar
KatB

Community Member, 94 Posts

23 January 2008 at 7:41pm

<% 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!:)

Avatar
KatB

Community Member, 94 Posts

25 January 2008 at 1:47pm

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>