Jump to:

3431 Posts in 1025 Topics by 864 members

Template Questions

SilverStripe Forums » Template Questions » <% if date = .... ?????

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

Page: 1 2
Go to End
Author Topic: 1408 Views
  • benni91
    Avatar
    Community Member
    72 Posts

    <% if date = .... ????? Link to this post

    Hi @all

    is it possible to control subpages like this?

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

    Thanks
    Benni

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: <% if date = .... ????? Link to this post

    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

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: <% if date = .... ????? Link to this post

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

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: <% if date = .... ????? Link to this post

    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, ... -
    - ******************************** -
    - -
    -----------------------------------------------------------------

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: <% if date = .... ????? Link to this post

    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 %>

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: <% if date = .... ????? Link to this post

    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

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: <% if date = .... ????? Link to this post

    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

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: <% if date = .... ????? Link to this post

    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?

    1408 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.