Wow, thanks again. I had assumed there was probably a better way of doing it - but had no idea where to begin. After only just getting to grips with how the old bit of code worked, I'll study that and try and work out how it works too.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
DataObjectManager Module /
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba
Sort By Category
- Previous 10 entries
- 1
- 2
- Page 33(current)
Can't work out how to get this leaner code to display properly in the template.
<% control PublicationYears %>
<h3>$Title</h3>
<% control Publications %>
<div class="Publication"><p>$Authors. ($Date.format(Y).) <a href="$Attachment.URL">$Name</a> $Description.</p>
<p class ="category">Type of Publication: $Category.</p></div>
<% end_control %>
<% end_control %>
Instead of:
2010
author. (2010.) publication name 4 description.
Type of Publication: category
author. (2010.) publication name 3 description.
Type of Publication: category
2009
author. (2009.) publication name 2 description.
Type of Publication: category
author. (2009.) publication name 1 description.
Type of Publication: category
I get:
publication 4
. (.) Publications .
Type of Publication:
publication 3
. (.) Publications .
Type of Publication:
publication 2
. (.) Publications .
Type of Publication:
publication 1
. (.) Publications .
Type of Publication:
Any ideas?
Sorry, I misread your code. You had it right. :)
Ah, OK. Thanks for all your help, and looking forward to the next update to the module!
Just an update on this in case anyone ever needs it.
Some 2011 publications were added to the page but were showing at the end, rather than the top. This was because I hadn't specified the order of the initial SQL query, this was easily fixed by adding 'order by Date desc' to the end.
$result = DB::query("SELECT DISTINCT YEAR(Date) as Year FROM `Publication` order by Date desc");
Another follow up. I upgraded this to use SS3, so it's not actually in DOM anymore, but for showing the object here's a much more elegant solution, adpated from: http://doc.silverstripe.org/framework/en/howto/grouping-dataobjectsets
class Publication extends DataObject
{
public function getYearCreated() {
return date('Y', strtotime($this->Date));
}
}
class PublicationPage_Controller extends Page_Controller
{
public function getGroupedPublicationsByDate() {
return GroupedList::create(Publication::get()->sort('Date desc'));
}
}
templates/includes/Publication.ss
<% loop getGroupedPublicationsByDate.GroupedBy(YearCreated) %>
<h3>$YearCreated</h3>
<% loop Children %>
<a class="publication sheet" href="$Attachment.URL">
<p>$Authors. ($Date.year) <strong>$Name</strong> $Description.</p>
<p>Type of Publication: $Category.</p>
</div>
</a>
<% end_loop %>
</div>
<% end_loop %>
- Previous 10 entries
- 1
- 2
- Page 33(current)