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.

All other Modules /

Discuss all other Modules here.

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

Conditional logic


Go to End


2 Posts   1756 Views

Avatar
Junglefish

Community Member, 109 Posts

14 October 2010 at 10:59pm

What's wrong with this code?

<% if InSection(administrative-news) %>
administrative-news
<% else_if InSection(image-galleries) %>
image-galleries
<% else %>
... alternative content ...
<% end_if %>

When the first conditional is true, this is what prints to the browser:
"administrative-news <% else_if InSection(image-galleries) %> image-galleries"

When the second conditional is true, this is what prints to the browser:
"... alternative content ..."

It's like the "else_if" clause is being interpreted as a string(!) What am I doing wrong?

jf/

Avatar
datswicked

Community Member, 15 Posts

24 June 2012 at 9:05pm

Edited: 24/06/2012 9:07pm

I believe you have to nest your if's as they don't work multi level (if, else if, else if, else) like php, only support 1 level (if, else) in templates..

ie i have done this as part of a product template and it works ok, bit of a hack but it works..

<% if Level(5) %>
	<!-- 5: PRODUCT  -->
	<% include prod_aCatProd %>
<% else %>
	<% if Level(4) %>
		<!-- 4: PRODUCT LISTING.. -->
		<% include prod_aCategory4 %>
	<% else %>
		<% if Level(3) %>
			<!-- 3: STORAGE  -->
			<% include prod_aCategory3 %>
		<% else %>
			<% if Level(2) %>
				<!-- 2: LANDING  -->
			<% end_if %>
		<% end_if %>	
	<% end_if %>
<% end_if %>

so you should have

<% if InSection(administrative-news) %>
                  administrative-news
<% else %>
               <% if InSection(image-galleries) %>
                  image-galleries
               <% else %>
                  ... alternative content ...
               <% end_if %>
<% end_if %>

then it will check the admin news first then the image galleries and then alternative content last

hope that helps. I know this is an old post but i came across it with no answer and hopefully it helps someone..