Jump to:

3447 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Help with Conditional logic

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

Page: 1
Go to End
Author Topic: 957 Views
  • Hello_electro
    Community Member
    80 Posts

    Help with Conditional logic Link to this post

    HI Everyone:

    I have a notice page and notice holder which I pull data from and place on the home page.

    I have them display based on the type of notice and if they are expired. I am trying to simply just place a line of text that states "no notices" if the notice type does not have any non expired pages.

    Everything is successful except, because I am using a control to show the data, if I use an If Else statement, the message appears the total amount of times i have notices. so since I am allowing 3 notices max to appear, the "no notices" message will appear three times. Anyone have suggestions where I can prevent this. here is my code:


    <div id="campusNotices">
    <% control SafetyNotices %>
    <% if InFuture(HomePageExpiration) %>
        <% if NoticeType==Safety %>
    <li><a href="$Link"><span>$NoticeDate&#58; $Title</span></a></li>
    <% end_if %>
    <% end_if %>
    <% end_control %>


    public function GetCampusNoticePage(){
    return DataObject::get("CampusNoticePage");


    function SafetyNotices($num=5) {
    $Notices = DataObject::get_one("CampusNoticeHolder");
    return ($Notices) ? DataObject::get("CampusNoticePage", "ParentID = $Notices->ID", "NoticeDate DESC", "", $num) : false;

    page.php < this is in the class section to allow this function to be used sitewide

    public function InPast($fieldname) {
    return $this->$fieldname < date('Y-m-d H:i:s');

    public function InFuture($fieldname) {
    return $this->$fieldname > date('Y-m-d H:i:s');


    class CampusNoticePage extends Page {

       public static $db = array(
          'NoticeDate' => 'Date',
          'NoticeType' => "Enum('Traffic, Safety','Traffic')",
          'HomePageExpiration' => 'Date',

       public static $has_one = array(

    function getCMSFields() {
    $fields = parent::getCMSFields();
           $fields->addFieldToTab("Root.Content.Main", new DatePickerField('NoticeDate'), 'Content');
           $fields->addFieldToTab("Root.Content.Main", new DropdownField( 'NoticeType' , 'Notice Type', array(
                'Traffic' => 'Traffic',
                'Safety' => 'Safety'   
                ),$this->NoticeType), 'Content');
           $fields->addFieldToTab("Root.Content.Main", new DatePickerField('HomePageExpiration'), 'Content');   
           return $fields;

    I know I am using the hell out of the IF?else statements so any suggestions would be great!

Page: 1
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.