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

Check page name in .ss file


Reply

4 Posts   801 Views

Avatar
Mauro74

11 July 2012 at 9:27am (Last edited: 11 July 2012 10:23am), Community Member, 30 Posts

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

11 July 2012 at 1:07pm Community Member, 16 Posts

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

11 July 2012 at 10:27pm Community Member, 135 Posts

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

12 July 2012 at 6:36am Community Member, 16 Posts

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