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

if and else_if with InSection()


Go to End


17 Posts   23014 Views

Avatar
Andrej

4 Posts

25 March 2009 at 8:43am

Gentlemen,

Why isn't this working?

    <% if InSection(home) %>
    <img src="/themes/BrightSide1/images/header.jpg" width="820" height="308" alt="headerphoto" class="no-border" />
    <% else_if InSection(services) %>
    <img src="/themes/BrightSide1/images/header_services.jpg" width="820" height="180" alt="headerphoto" class="no-border" />
    <% else %>    
    <img src="/themes/BrightSide1/images/headerphoto.jpg" width="820" height="180" alt="headerphoto" class="no-border" />

    <% end_if %>

Here's what I want to do: I have a large header photo for the home page (that works)
Then, I want some pages to have a different header photo (services, about us and team)
I also want a fall-back photo for all the other pages (hence the final else statement)

Thanks for your help!

Avatar
Double-A-Ron

Community Member, 607 Posts

25 March 2009 at 3:06pm

Strange.

Appears to be the else_if statement not liking having a method call as a condition.

I tried this and it worked fine:

<% if InSection(home) %>
Home
<% else_if 1 %>
Test
<% else %>
Not Either Page
<% end_if %>

Check this out: http://www.silverstripe.org/archive/show/138468#post138468.

Quick ugly hack:

<% if InSection(home) %>
	<img src="/themes/BrightSide1/images/header.jpg" width="820" height="308" alt="headerphoto" class="no-border" />
<% else %>
	<% if InSection(services) %>
    <img src="/themes/BrightSide1/images/header_services.jpg" width="820" height="180" alt="headerphoto" class="no-border" /> 
  <% else %>
  	<img src="/themes/BrightSide1/images/headerphoto.jpg" width="820" height="180" alt="headerphoto" class="no-border" /> 
  <% end_if %>
<% end_if %>

Aaron

Avatar
Andrej

4 Posts

26 March 2009 at 7:20am

Thanks, that worked. It's ugly though ... especially if you have multiple "if" statements.

Avatar
Double-A-Ron

Community Member, 607 Posts

26 March 2009 at 8:28am

Yup, see the link for how someone else fixed it without the template-side hack.

Perhaps a bug report was never raised on this, so you might want to add it yourself.

Cheers
Aaron

Avatar
Double-A-Ron

Community Member, 607 Posts

26 March 2009 at 10:20am

I have submitted a ticket for this:

http://open.silverstripe.org/ticket/3785

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 2:23am

Edited: 27/03/2009 2:24am

Hi,

Background on this: Template code parsing is done in sapphire/core/SSViewer.php. The function parseTemplateContent runs template code through a series of regular expressions which replace template code constructs with PHP code. These regular expressions support an arbitrary combination of call signatures. The call you are trying to make isn’t one of the supported call syntaxes.

A patch has been submitted for testing which would remove these arbitrary constraints. So, code like <% else_if Call1(Arg1) %> should work. Would you have time to help test this patch?

Thank you,
Ben

Avatar
Double-A-Ron

Community Member, 607 Posts

27 March 2009 at 9:01am

Cheers Ben,

Just downloaded patch revision5 and applied to my SVN. Will test a few things over the weekend and recomment my bug submission.

Thanks again
Aaron

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 9:13am

Thanks, Aaron! Please let me know what you find.

Ben

Go to Top