Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » conditional content

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1663 Views
  • wookieecookies
    Avatar
    2 Posts

    conditional content Link to this post

    hello,

    i am attempting to take an existing website and recreate it using silverstripe. i'm relatively new to the programming side of it so this may be a simple problem that im completely overlooking the simple solution to.

    i have a StaffPage (same as EmployeePage) which contains a the following:

    ------
    Employee Name
    Email: employee_email@whatever.com
    <several paragraphs of bio text>
    ------

    my StaffPage has input fields for the "Content" (bio text), "Name" and "Email". some of the employees do not have an email address and i would like to make that part of the html conditional based on whether information is in the field

    the code for my StaffPage template is:

    ------------------------

    <% if Menu(2) %>
    <% include Menu3 %>
    <% end_if %>
       <div id="StaffPhoto">
          $Photo.SetWidth(340)
       </div>
       <h1 id="pagetitle">$Title</h1>
       <a href="mailto:$Email">Email: $Email</a>
       $Content

    -----------------------

    i have tried adding:

    -----------------------

    <% if $Email %>
       <a href="mailto:$Email">Email: $Email</a>
    <% end_if %>

    -----------------------
    but the syntax there is incorrect and gives an error

    can anyone tell me how to correctly do this? or point me in the right direction? i have tried searching for more help on conditions but couldnt seem to find any in-depth documentation

    thanks in advance!

  • trevor
    Avatar
    53 Posts

    Re: conditional content Link to this post

    what you have to do is define a function in your pagetype.php file that returns true or false if the email exists for example

    function hasEmail() {
    if ($this->Email == "") then return false;
    else
    return true;
    }

    then in the template you call the function <% if hasEmail %>

    There might be another way but I have done a similar thing this way and it works.

    Regards

    Trevor

  • wookieecookies
    Avatar
    2 Posts

    Re: conditional content Link to this post

    that worked! thanks very much trevor

    1663 Views
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.