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

Check page name in .ss file


Go to End


4 Posts   2690 Views

Avatar
Mauro74

Community Member, 30 Posts

11 July 2012 at 9:27am

Edited: 11/07/2012 10:23am

Hi all, I'm new to Silverstipe so be patient please if the question has a very easy answer :)

I want to check – in the template – the page name or title in order to show a different image. I'd like to do something like that:

<% if $SiteConfig.Title == 'video' %>
                <img src="/themes/blackcandy/images/image.jpg" />
<% else if $SiteConfig.Title == 'bio' %>
                <img src="/themes/blackcandy/images/image1.jpg" />
...
...

<% end_if %>

How do I do it? Is there a more efficient way of doing it?

Thanks a lot.
Mauro

Avatar
jaredkipe

Community Member, 16 Posts

11 July 2012 at 1:07pm

Which version of SilverStripe are you using?

Typically this would be something you accomplish in the controller. Namely write a controller method that either returns the title of the page, or rather write a controller method that returns the path to the image (and let the controller method go through the possibilities).

That said, it is reasonably easy to add attributes to pages that would allow you to specify say an image uri to act as the thumbnail for that page or something too.

Avatar
Nobrainer Web

Community Member, 138 Posts

11 July 2012 at 10:27pm

Hi Mauro74,

If you just want to check against a page title, you should not use SiteConfig, rather just (note that the $ sign is not used in if and controls)
<% if Title == 'video' %>
Page title is: $Title
<img src="/themes/blackcandy/images/image.jpg" />
<% else if Title == 'bio' %>
Page title is: $Title
<img src="/themes/blackcandy/images/image1.jpg" />
<% end_if %>

You can also add a field for each page, allowing the admin to specify a image in the backend for each page.
See the tutorials: doc.silverstripe.org

Avatar
jaredkipe

Community Member, 16 Posts

12 July 2012 at 6:36am

Note that different SilverStripe versions will have different syntax for comparisons in the template markup.