17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1570 Views |
-
Children: Controllers and templates

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?
-
Re: Children: Controllers and templates

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?
-
Re: Children: Controllers and templates

23 January 2008 at 4:38pm
There is clearly some communication going when you have $TotalItems and $Pos.
-
Re: Children: Controllers and templates

23 January 2008 at 5:34pm Last edited: 23 January 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?
-
Re: Children: Controllers and templates

23 January 2008 at 7:22pm Last edited: 23 January 2008 7:23pm
http://doc.silverstripe.com/doku.php?id=built-in-page-controls
maybe this will help you?
-
Re: Children: Controllers and templates

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.AsdfYAY!
-
Re: Children: Controllers and templates

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 "{$Title}"">$Title</a></h3>
<p class="newsSummary">$Content.FirstParagraph <a href="$Link" title="Read more on "{$Title}"">Read more >></a></p><% if Parent.endDate %></div><!-- end of div.newsArticle --><% end_if %>
</div>
<% end_control %>
</ul>
</div>
| 1570 Views | ||
|
Page:
1
|
Go to Top |

