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.

Template Questions /

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

<% if date = .... ?????


Go to End


16 Posts   3547 Views

Avatar
benni91

Community Member, 72 Posts

29 July 2011 at 6:58am

Hi @all

is it possible to control subpages like this?

<% if date < 01.01.2009 %>
Template 1
<% else %>
Template 2
<% end_if %>

Thanks
Benni

Avatar
zenmonkey

Community Member, 545 Posts

29 July 2011 at 8:01am

Hmm I don't think you can use operator in the template if statements, but you could always create a method on your Page. This is assuming you're trying to just render the certain page itms in a template differntly

function OldPage() {
$date = $this->Date;
if ($date->Year() < 2009){
return true;
} else {
return false;
}
}

This assumes you've saved the Date field in a proper date format and not as a some custom string. then you can use this in your template
<% if OldPage %>

<% else %>

<% end_if %>

If you're trying to use a totally different page templates to render older pages just use the date check in your page index function


public function index() {
$date = $this->Date;
if ($date->Year() < 2009){
return $this->renderWith("OldPage");
} else {
return Array();
}

This will render old pages with OldPage.ss while new pages with the default

Avatar
benni91

Community Member, 72 Posts

29 July 2011 at 8:10am

i think the first option should work. i'll try it now and post my result in a view minutes :)

Avatar
benni91

Community Member, 72 Posts

29 July 2011 at 8:22am

Edited: 29/07/2011 8:51am

hmm it didn't work. but i think i know why. because i'll use the function? if control? (don't know the right name) on the parent page? the date is written in a child page.

this is the code:

ReferencePage.php http://pastie.org/2286299 - where the date is saved
ReferencesPage.php http://pastie.org/2286316 - where i want to use the seperation by date
ReferencesPage.ss http://pastie.org/2286319

i put your function in the ReferencePage.php and then i tried to use it inside <% control Children %>

but it didn't work :(

EDIT// can i also use the function like this

function OldPage() {
$date = $this->Date;
if ($date->Year() =< 2009){
return true;
} else {
return false;
}
}

so everything with was saved in 2009 and before ?

EDIT2// The final Page should look like this:

------------------------------------------------------------------
- All References -
- -
- ########################## -
- After 2009 -
- New Ref 1, New Ref 2, New Ref 3, ... -
- ########################## -
- -
- ******************************** -
- Before 2009 -
- Old Ref1, Old Ref2, Old Ref3, ... -
- ******************************** -
- -
-----------------------------------------------------------------

Avatar
zenmonkey

Community Member, 545 Posts

29 July 2011 at 9:04am

My Fault, the Date Field isn't saved as Date Object this is teh function you need to use

function NewPage(){
		$date = substr($this->ReleaseDate, 0, 4); 
		if ($date >= 2009){ 
			return true; 
		} else { 
			return false; 
		} 
	}

The date is saved in the following format YEAR-MO-DA so you need only pull the first 4 characters in the string and see if they're less than 2009

Put that in your RefrencePage (not in the RefrencePage_controller)

then in RefrencesPage.ss you can use

<% control Children %>
<% if NewPage %>
New Refrence COde
<% else %>
Old Refrence Code
<% end_if %>
<% end_control %>

Avatar
benni91

Community Member, 72 Posts

30 July 2011 at 4:57am

Edited: 30/07/2011 5:12am

what should i put in which string? how can i change the dateformat to DA.MO.YE ?

// At the moment it doesn't work :/ the output is always the whole references :(

Avatar
zenmonkey

Community Member, 545 Posts

30 July 2011 at 5:05am

Edited: 30/07/2011 5:19am

If you change the date format you will need to change the substr() function.

DateFormat is set at the User Level, but I'm not sure if you change the content authors Date setting if it will save the data in the revised format or just output differently to the end user

http://doc.silverstripe.org/sapphire/en/topics/configuration

EDIT
Just did a quick check and my date is up as MO/DA/YEAR but the Date field saves as YEAR-MO-DA

But you can use $Date.NiceUS to change it in the template

Avatar
benni91

Community Member, 72 Posts

30 July 2011 at 5:14am

Edited: 30/07/2011 5:22am

Have you read my edit?
// At the moment it doesn't work :/ the output is always the whole references

in the admin interface my date output is DD-MO-YYYY. could this be the problem?

Go to Top