Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Trying to create custome image SRC field from $Title

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

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

Page: 1
Go to End
Author Topic: 1528 Views
  • lubzee
    Avatar
    Community Member
    15 Posts

    Trying to create custome image SRC field from $Title Link to this post

    I want to reference images based on page titles as in:

    <img src="$Title.jpg" />

    How can I escape the $Title part so that it would render the page title and extension as one string. As in Home.jpg

    Is this possible?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Trying to create custome image SRC field from $Title Link to this post

    You need to add { } around the variable to tell SS what it is. Eg at the moment it will be looking for a field $Title.jpg which is wrong. You need to do {$Title}.jpg

  • lubzee
    Avatar
    Community Member
    15 Posts

    Re: Trying to create custome image SRC field from $Title Link to this post

    Thanks for the quick reply...

    Would this be the 'correct' way to do something like this.. I can't maybe create a custom property from the controller side that would add the extension and output the final string and just call it as something like $TitleImage?

    I'm still fairly new to this great CMS and mvc concepts

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Trying to create custome image SRC field from $Title Link to this post

    You could do but that would be over designing it. Tho the 1 thing I would say is you can have spaces in titles, a safer option would be to use $URLSegment which would return things like 'home', 'about-us', 'blog' etc and $URLSegment doesn't has spaces - Images URLs would fail with spaces.

    1528 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.