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.

Template Questions /

if and else_if with InSection()


Go to End
Reply


17 Posts   12312 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, 604 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: [url]http://www.silverstripe.org/archive/show/138468#post138468[/url].

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, 604 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, 604 Posts

26 March 2009 at 10:20am

I have submitted a ticket for this:

[url]http://open.silverstripe.org/ticket/3785[/url]

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 [url=http://open.silverstripe.org/ticket/3738]patch[/url] 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, 604 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