Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Check page name in .ss file

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

Page: 1
Go to End
Author Topic: 663 Views
  • Mauro74
    Avatar
    Community Member
    30 Posts

    Check page name in .ss file Link to this post

    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

  • jaredkipe
    Avatar
    Community Member
    16 Posts

    Re: Check page name in .ss file Link to this post

    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.

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Check page name in .ss file Link to this post

    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

  • jaredkipe
    Avatar
    Community Member
    16 Posts

    Re: Check page name in .ss file Link to this post

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

    663 Views
Page: 1
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.