Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Conditionally inserting image

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: 1411 Views
  • gakenny
    Avatar
    Community Member
    153 Posts

    Conditionally inserting image Link to this post

    Hello,

    I would like to conditionally insert an image into Page.ss based on the value of $URLSegment and the presence of the image file:

    First problem:
    I am trying to generate the HTML to display the image and I have inserted the following code into Page.ss.

    <img class="center" src="assets/images/$URLSegment.jpg" alt="$Title" width="520" />

    However, $URLSegment and .jpg is replaced with blank, ie. src=""assets/images/"

    Second problem:
    How do I detect the existence of this file in Page.ss so as to only insert the relevant HTML if the file exists?

    Cheers,

    Gary

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Conditionally inserting image Link to this post

    Hello,

    To resolve this issue I inserted $URLSegment inside curly brackets ('{$URLSegment}').

    I hope this helps.

    Cheers,

    Gary

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Conditionally inserting image Link to this post

    Optionally, you could make a new function called ImagePath() on your class and use $ImagePath in the template, instead. This would automatically find the relative path, like assets/images/$URLSegment, so you just need to insert <img src="$ImagePath" alt="My image" /> - this avoids the problem of having curly braces in your nice templates.

    function ImagePath() {
    return 'assets/images/' . $this->URLSegment . '.jpg';
    }

    <% if ImagePath %>
    <img src="$ImagePath" alt="My image" />
    <% end_if %>

    Cheers,
    Sean

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Conditionally inserting image Link to this post

    Thanks Sean - I might just try that.

    Initially, I wasn't sure if I could pass a variable back from a function. I have been plugging away at the templating engine at the weekend and have made some breakthroughs - thanks to your support!

    Many thanks,

    Gary

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