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.

Archive /

Our old forums are still available as a read-only archive.

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

InSection Questions


Go to End


5 Posts   3901 Views

Avatar
Liam

Community Member, 470 Posts

9 August 2008 at 7:47am

I'm guessing it is not possible to check multiple pages using InSection? e.g if InSection(page1,page2)

Is it then possible to use if/else if statements in my template with it? I tried with no luck.

<% if InSection(page) %>
     Something
<% else_if InSection(page2) %>
     Something else
<% end_if %>

Avatar
vstrazz

Community Member, 63 Posts

9 August 2008 at 8:34am

try this

<% if URLSegment== page title %>
content here
<% else_if URLSegment== page title 2 %>
more content
<% end_if %>

Avatar
Willr

Forum Moderator, 5523 Posts

9 August 2008 at 10:02pm

I would have thought your (Lee's) code would work. Does something like this work?

<% if InSection(a) %>
<% else %>
<% if InSection(b) %>
<% end_if %>
<% end_if %>

Avatar
Billy_

Community Member, 17 Posts

11 August 2008 at 9:05am

Edited: 11/08/2008 4:59pm

I just had the same issue as LeeUmm while using SS v2.2.2. I found a possible bug in sapphire/core/SSViewer.php and a fix.

Around line 263 in SSViewer.php you'll find several ereg_replace() calls that appear to change the .ss template if/else syntax to php if/else syntax. There are a few lines that replace "else_if" wrong or not at all...

// line 278
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *&&? *([A-Za-z0-9_]+) +%' . '>', '<? else_if($item->hasValue("\\1") && $item->hasValue("\\2")) { ?>', $content);
//change to
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *&&? *([A-Za-z0-9_]+) +%' . '>', '<? } else if($item->hasValue("\\1") && $item->hasValue("\\2")) { ?>', $content);

// line 274
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *\\|\\|? *([A-Za-z0-9_]+) +%' . '>', '<? else_if($item->hasValue("\\1") || $item->hasValue("\\2")) { ?>', $content);
// change to 
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+) *\\|\\|? *([A-Za-z0-9_]+) +%' . '>', '<? } else if($item->hasValue("\\1") || $item->hasValue("\\2")) { ?>', $content);

// line 266
$content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\(([A-Za-z0-9_-]+)\\) +%' . '>', '<? if($item->hasValue("\\1",array("\\2"))) {  ?>', $content);
// ADD this line below it...
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+)\\(([A-Za-z0-9_-]+)\\) +%' . '>', '<? } else if($item->hasValue("\\1",array("\\2"))) {  ?>', $content);

// line 263
$content = ereg_replace('<' . '% +if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', '<? if($item->obj("\\1",null,true)->hasValue("\\2")) {  ?>', $content);
// ADD this line below it...
$content = ereg_replace('<' . '% +else_if +([A-Za-z0-9_]+)\\.([A-Za-z0-9_]+) +%' . '>', '<? } else if($item->obj("\\1",null,true)->hasValue("\\2")) {  ?>', $content);

Keep in mind this changes core code. Any upgrades to SS will erase these changes. By then this bug might be fixed anyway :). I will try to look in the bug reports to see if it's there already, and if not add it.

Oh, and Willr's suggestion works for me, I just wanted to have the ability to make my templates a bit more readable.

-Billy

Avatar
Liam

Community Member, 470 Posts

12 August 2008 at 11:32am

Great find and fix. I haven't had time to apply changes, just viewing the topic for the first time since posting with the weekend and all.

Submitting the bug would be great, as I'm like you, I'd rather have cleaner more proper code in my templates, and having to update all my SS sites in the future would be a hassle.