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.

Archive /

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

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

if else statements


Reply


7 Posts   3205 Views

Avatar
noizy

Community Member, 25 Posts

21 February 2007 at 2:47pm

I see from the templates page...

http://doc.silverstripe.com/doku.php?id=templates#template_syntax

...that you can't use elseif statments. It either is, or it isn't. That's okay, but what's confusing me is the use of if statments that don't just check for the existence of an object, but which check it against a value.

According to the docs "...the optional content will only be shown if the requested field / method returns the value you specify. You should *not* include quotes around the value."

So that means no statements like...

<% if Page.URLSegment = "sectiontitle" %>

Correct? Or, indeed, no string comparisons at all?

Anyway, having given up on strings for now, I figured I could at least make an integer comparison. I have a contacts block that gets included on all the pagetypes in one branch of my tree hierarchy, and I want all the levels in that branch to display the custom contact details that I've created at the top level pagetype. My include originally looked like this...

   <% if Level(2) %>
      <h2>$Title</h2>
      Phone: $ContactPhone<br />
      Email: $ContactEmail<br />
   <% end_if %>

   <% if Level(3) %>
      <h2>$Parent.Title</h2>
      Phone: $Parent.ContactPhone<br />
      Email: $Parent.ContactEmail<br />
   <% end_if %>

   <% if Level(4) %>
      <h2>$Parent.Parent.Title</h2>
      Phone: $Parent.Parent.ContactPhone<br />
      Email: $Parent.Parent.ContactEmail<br />
   <% end_if %>

...which works ok at Level(2), but the subsequent levels also return true for each statement above it, so at the bottom level I end up with three sets of contact details (correct for the level I'm actually on - otherwise blank).

So, I thought, all I need to do is find the ID of the top level page, and use that instead of Level(x) . The page in question had an ID of 5, so I tried...

   <% if Parent.ID = 5 %>
      <h2>$Title</h2>
Phone: $ContactPhone<br />
      Email: $ContactEmail<br />
   <% end_if %>

...which, unfortunately, crashes the page. If, however, I take off the "= 5 " bit of the IF statement, the code runs fine (if not actually giving the result I want, as it will always return TRUE). This would also make me think that Parent.ID exists as a method on the page. If I add this html...

<p>This page's parent's ID is: $Parent.ID</p>

...at the top of my contacts include file, it displays the correct number. So it's the "= 5" bit that seems to the problem. I'm assuming it's a syntax issue or something, but I just can't quite figure out how to get it right. Any suggestions?

Avatar
Sean

Forum Moderator, 922 Posts

21 February 2007 at 7:59pm

What you've forgotten in your syntax is using <% control Level(4) %> inside your if, all the if is doing is checking if the current page meets such criteria. However, if you're wanting to get data from other pages like get everything from Level(4) you need to add the controls inside. ie:

<% if Level(4) %>
<% control Level(4) %>
Phone: $ContactPhone
<% end_control %>
<% end_if %>

Avatar
Sean

Forum Moderator, 922 Posts

21 February 2007 at 8:00pm

Sorry, you don't _need_ the if around the control at all if you're wanting data from other pages on another. Otherwise it won't work, so...:

<% control Level(4) %>
Phone: $ContactPhone
<% end_control %>

..should work.

Avatar
noizy

Community Member, 25 Posts

22 February 2007 at 9:33am

I tried it with and without the IF Level(x) statements, and the behaviour remains the same. On a Level 4 page, this code...

   <% if Level(2) %>
   <% control Level(2) %>
You're on level 2
   <% end_control %>
<% end_if %>

      <% if Level(3) %>
   <% control Level(3) %>
You're on level 3
   <% end_control %>
   <% end_if %>

      <% if Level(4) %>
   <% control Level(4) %>
You're on level 4
   <% end_control %>
   <% end_if %>

returns...
You're on level 2
You're on level 3
You're on level 4

Same result if I leave the IF...END_IF statements out.

I've actually got around the problem by (at Sam's suggestion) writing a function in the relevant page's php code file, but it'd be good to know how the page is actually handling those control and if commands on Level(x) for future reference.

Avatar
someonlinedude

Community Member, 11 Posts

22 February 2007 at 2:30pm

Just for reference more infos about the in page controls.

http://doc.silverstripe.com/doku.php?id=built-in-page-controls

Avatar
Andy

230 Posts

26 February 2007 at 3:39pm

I think what you're trying to do is

<% control Level(2) %>
<h2>$Title</h2>
Phone: $ContactPhone <br />
Email: $ContactEmail <br />
<% end_control %>

Level(2) doesn't check whether the current page is in level 2, it returns the page in the site tree on the second level that the current page is a descendant of. So if the current page is on the second level, it would return the current page, if it was on the third level, it would return the pages parent, if it was on the first level it wouldn't return anything.

As far as I am aware, you can't make comparisons in templates, only check for the existence of controls/variables. Logic should be kept out of the templates, so generally the best solution is to write a function on the data object or controller.

Avatar
Andy

230 Posts

26 February 2007 at 5:02pm

Hmm, according to http://doc.silverstripe.com/doku.php?id=built-in-page-controls I'm wrong about not being able to do comparisons in the templates...