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.

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.

Customising the CMS /

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

Loop over Children and display data when Date is greater than DateToday


Go to End


7 Posts   1926 Views

Avatar
whjonker

Community Member, 12 Posts

23 May 2016 at 7:45pm

I am currently working on a one page website. I have customized the homepage template for that. This page has different sections. For one section it should display the time and dates of a band performance. I created a new template (PerformancePage.ss) for this and created a loop in the HomePage.ss.

<% loop $Children %>
<% include PerformancePage %>
<% end_loop %>

This works perfectly fine, but what it might occur that there are no performances. I thought I could do this by looking at the performance date. When $Performance_Date > Today it should display the performance, ELSE it should display a custom message.
I tried this on the PerformancePage.ss

<!-- Performance Starts -->
<% if $Performance_Name == "test1" && $Performance_Date > DateTodayValue %>
<div class="col-md-3 col-sm-6 single-pricing-wrap center animated" data-animation="bounceInLeft" data-animation-delay="500">
<div class="single-pricing">
<div class="pricing-head">
<h4 class="pricing-heading color-scheme">$Performance_Name</h4>
<div class="price">
<h3> $Performance_Date </h3>
</div>
</div>
<ul class="package-features">
<li><span class="color-scheme fa fa-clock-o"></span>Tijd: $Performance_Start_Time - $Performance_End_Time uur</li>
</ul>
<div class="sign-up"> <a href="$Performance_Link" target="_blank" class="fancy-button button-line btn-col zoom"> Koop tickets <span class="icon"> <i class="fa fa-ticket"></i> </span> </a> </div>
</div>
</div>
<% else %>
There are currently no performances
<% end_if %>
<!-- Performance Ends -->

I tested it by populating the $Performance_Name value with “test1” and it did not work. Ideally it should look at the $Performance_Date. But I don’t know how I can do this.

Can anyone help me with this?

Thank you!

Wesley

Avatar
martimiz

Forum Moderator, 1391 Posts

24 May 2016 at 3:26am

I'm not quite sure how you've set this up - is it so that one of your HomePage's children is a performance page, representing one performance? Or are there multiple PerformancePages, each a child of your HomePage? Or are performances DataObjects linked to a performancepage that in turn is a child of HomePage?

I think I would need more info :)

Avatar
whjonker

Community Member, 12 Posts

24 May 2016 at 6:41am

Hi Martimiz,

The setup is as follows:

Directory: mysite/code/HomePage.php
Directory: mysite/code/PerformancePage.php
Directory: themes/themename/HomepagePage.ss
Directory: themes/themename/PerformancePage.ss

So under Main directory HomePage.ss I currently have 4 childeren (4x PerformancePage.ss)

And PerformancePage.ss has code that I was referring to.

Sorry for not being clear the first time and thank you in advance for your help. :)

Wesley

Avatar
martimiz

Forum Moderator, 1391 Posts

24 May 2016 at 8:33pm

Supposing 'Performance_Date' is a Date field, have you tried something like <% if $Performance_Date > $Now %>

Avatar
whjonker

Community Member, 12 Posts

24 May 2016 at 9:49pm

Thank you Martimiz. This is almost showing me the behavior that I was expecting. Do you happen to know if I can add these conditions to the loop in the HomePage.ss

For Example:

<% loop $Children %>
<% if $Date > $Now %>
<% include PerformancePage %>
<% else %>
There are currently no performances
<% end_if %>
<% end_loop %>

When I add this.It works but somehow it is then showing two performance items that have date > Now and the "There are currently no performances" message. I am almost there but I think I am missing something.

Thanks again!

Wesley

Avatar
martimiz

Forum Moderator, 1391 Posts

25 May 2016 at 12:13am

That's because you check the date for each child, and everytime a child is too old, you generate that message. What you would probably want is to loop a set of valid performances only instead of all Children, and if there are none, display your message.

So for instance in your Homepage controller something like:

public function getValidPerformances() 
{
    return PerformancePage::get()
        ->filter(array('Performance_Date:GreaterThan' => date('Y-m-d')))
        ->sort('Performance_Date ASC');
}

And then in your template:

<% if $ValidPerformances %>
    <% loop $ValidPerformances %>
       <% include PerformancePage %>
    <% end_loop %>
<% else %>
    There are currently no performances 
<% end_if %>

Of course, if your homepage has other children besides PerformancePages, this setup will still need some tweaking :)

Avatar
whjonker

Community Member, 12 Posts

25 May 2016 at 1:11am

Hi Martimiz,

This is exactly what I was looking for. This works like a charm.

Thank you very very much! You don't have an idea how long I have been struggling with this :)

Wesley