Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » if and else_if with InSection()

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

Page: 1 2 3
Go to End
Author Topic: 11585 Views
  • Andrej
    Avatar
    4 Posts

    if and else_if with InSection() Link to this post

    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!

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: if and else_if with InSection() Link to this post

    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

  • Andrej
    Avatar
    4 Posts

    Re: if and else_if with InSection() Link to this post

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

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: if and else_if with InSection() Link to this post

    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

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: if and else_if with InSection() Link to this post

    I have submitted a ticket for this:

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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: if and else_if with InSection() Link to this post

    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

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: if and else_if with InSection() Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: if and else_if with InSection() Link to this post

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

    Ben

    11585 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.