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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Has else_if been removed from the template language?


Go to End


7 Posts   3100 Views

Avatar
MarcusDalgren

Community Member, 288 Posts

4 August 2009 at 4:57am

I found an old post about control structures in templates that states that else_if should work but when I try to use it in my templates it just gets printed as raw text.

I tried doing something like this

<% if Level(2) %>
  $Parent.Title
  <% else_if Level(3) %>
  $Title
  <% else %>
  $Title
  <% end_if %>

But I get the else_if back in raw text.

Avatar
jaybee

Community Member, 49 Posts

28 May 2010 at 11:21am

I'm having the same problem, does else_if work or not? What's the deal with it just coming out as plain text? My code is:

<% if InSection(home) %>
 home
<% else_if InSection(about) %>
 about
<% else %>
 subpage
<% end_if %>

Avatar
Junglefish

Community Member, 109 Posts

14 October 2010 at 11:21pm

Did this ever get answered? I have the same problem, see here: http://www.silverstripe.org/all-other-modules/show/293853?showPost=293853

jf/

Avatar
swaiba

Forum Moderator, 1899 Posts

20 October 2010 at 2:39am

I'd raise this as a bug as I've seen it too!

But it is soooo simple to fix that I hadn't bothered...

<% if InSection(home) %> 
	home 
<% else %>
	<% if InSection(about) %> 
		about 
	<% else %> 
		subpage 
	<% end_if %>
<% end_if %>

Avatar
Junglefish

Community Member, 109 Posts

20 October 2010 at 2:49am

@Swaiba

That's all well and good if you only have 3 cases. Unfortunately I have about 8 which leads to a very messy set of nested conditionals which goes something like this:

<% if InSection(home) %>
home
<% else %>
<% if InSection(about) %>
about
<% else %>
<% if yyyyyyyyyyyyyyyyyyyy %>
subpage
<% else %>
<% if xxxxxxxxxxxxxx %>
something else
<% else %>
<% if zzzzzzzzzzzzzzzzz %>
something else
<% else %>
something else
<% end_if %>
<% end_if %>
<% end_if %>
<% end_if %>
<% end_if %>

And that's the simplified version(!)

So, does anyone know, has "else_if" actually been removed???

jf/

Avatar
swaiba

Forum Moderator, 1899 Posts

20 October 2010 at 3:51am

Edited: 20/10/2010 3:51am

Oh Absolutely Junglefish!

As I said "I have noticed else_if not working as well" and if you want to get an official response then it is always easier to go to http://open.silverstripe.org/ and raise a ticket!

IMHO if you have such a large complex if / else if I'd think it was too complex and try and achieve it slightly differently using different page types, includes or involving the controller a little more. Or you could do the following...

<% if InSection(home) %> 
home 
<% end_if %>
<% if InSection(about) %> 
   about 
<% end_if %>
<% if yyyyyyyyyyyyyyyyyyyy %> 
   subpage 
<% end_if %>
etc..

or without the formating if you have a small amount of code inside each if (and if not, perhaps including templates might help)...

<% if InSection(home) %>home<% end_if %>
<% if InSection(about) %>about<% end_if %>
<% if yyyyyyyyyyyyyyyyyyyy %>subpage<% end_if %>
etc..

And yes that also has some flaws... Personally I'd recommend trying to do this work without such a large/complex logic in the template. There are many other ways to do things in silverstripe, as yet another suggestion you could extend page to add to the $db a field MyCustomText and then for each of these page types you can edit the text in the cms and then you only need $MyCustomText in the template.

Just to make it clear I agree that else_if is buggy, but I am also suggesting that there are other, perhaps better ways, to get the same result.

Avatar
(deleted)

Community Member, 473 Posts

20 October 2010 at 12:25pm

<% else_if %> has not been removed, it just doesn't accept as much options as <% if %> does. The valid ways of using <% else_if %> are:

  • <% else_if Val %>
  • <% else_if Val1 || Val2 %>
  • <% else_if Val1 && Val2 %>
  • <% else_if Val1 == Val2 %>
  • <% else_if Val1 != Val2 %>
  • <% else_if Obj.Val %>
  • <% else_if Obj.Meth.Val %>