Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » if-statement for variables, strings etc.

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: 3493 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    if-statement for variables, strings etc. Link to this post

    Hi Everyone

    I have a smaller problem that I need some help with. And it should be really simple.

    I want to create an if statement for variables in my classes/templates, just like I do with menus in the template, i.e.

    <% if Menu(2) %>
       <div id="mySecondMenu">
          <% control Menu(2) %>
          ...
           <% end_control %>
    </div>
    <% end_if %>

    So what I basically would do is this:

    <% if $myImage %>
       <img src="$myImage"/>
    <% end_if %>

    Does it make sense?

    I'll try to explain. Let's say for instance that I've created an image in a template (both in the class file and template), but the user doesn't fill it out in the backend. Now of course the image doesn't show up in front, because there is no, instead the browser is showing me a questionmark, and that looks pretty silly.

    Can anyone help, thanks.

    Joel

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: if-statement for variables, strings etc. Link to this post

    Ah,

    Remove the dollar sign. Your template should look like:

    <% if myImage %>
    <img src="$myImage"/>
    <% end_if %>

    Should work fine

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: if-statement for variables, strings etc. Link to this post

    Great, works! Thanx...

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: if-statement for variables, strings etc. Link to this post

    Ok, one more question though.

    I've tried to make two checks with the && sign, and that works too.

    Now, is there a way to write "or", something like this:

    <% if MyFirstVariable or MySecondVaribale %> //Where either the first or second variable is true?

    Thanks again...

    3493 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.