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?