Jump to:

23552 Posts in 19361 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Set Default image if the image fetched is not found

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 866 Views
  • Juan
    Avatar
    Community Member
    18 Posts

    [SOLVED] Set Default image if the image fetched is not found Link to this post

    Please help. how i can do this on SS template?
    <% control Parent.function %>
    $blogimage = $Image.URL

    <% if $image.url == not found % >

    $blogimage = "src/Default_img.jpeg"

    <%end_if %>
    <% end_control %>

    how to identify if the image is not found? is there a variable that can be use?

    Thanks in advance

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    Hi Juan.

    Are you using SilverStripe V3? If you are use <% loop %> instead of <% control %>.

    <% loop Parent.function %>
    <% if $Image %>
    $Image
    <% else %>
    <img src="$Themedir/images/your-image.jpg" alt="" />
    <% end_if %>
    <% end_loop %>

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    Hello IOTI

    thanks for the reply..

    no im using v 2.4

    i have try this solution but its still not working.. :|

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    Juan

    You may have to share a little more info with me. Is anything being returned?

    What is "<% control Parent.function %> " suppose to be/return?

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    yes.. ofc

    it will return the latest blogpost including

    $title, $Image, $Link etc.

    now my problem is.. image urls is saved in the db but the pictures is no longer exist in the uploads directory and i want to set default image if its not found.. i dont want to use Jquery.

    i have try this. but it doesnt work.. any other solution?

    <% if $Image %>
    $Image
    <% else %>
    <img src="$Themedir/images/your-image.jpg" alt="" />
    <% end_if %>

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    Hey Juan

    Ah awesome, now I understand a little more

    Looking through the Image.php code there is a function called getTag() that checks if the file exists. This is untested code but I believe it should work.

    <% if Image.getTag %>
    $Image
    <% else %>
    <img src="$Themedir/images/your-image.jpg" alt="" />
    <% end_if %>

    if that doesn't work try

    <% if Image.getTag != NULL %>
    $Image
    <% else %>
    <img src="$Themedir/images/your-image.jpg" alt="" />
    <% end_if %>

  • Juan
    Avatar
    Community Member
    18 Posts

    Re: [SOLVED] Set Default image if the image fetched is not found Link to this post

    It worked!

    thank you OITI!

    Highly appreciated!

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