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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] Set Default image if the image fetched is not found


Go to End


7 Posts   3034 Views

Avatar
Juan

Community Member, 18 Posts

21 February 2013 at 10:05pm

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

Avatar
copernican

Community Member, 189 Posts

22 February 2013 at 2:22am

Edited: 22/02/2013 2:23am

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 %>

Avatar
Juan

Community Member, 18 Posts

22 February 2013 at 4:06am

Hello IOTI

thanks for the reply..

no im using v 2.4

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

Avatar
copernican

Community Member, 189 Posts

22 February 2013 at 9:11am

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?

Avatar
Juan

Community Member, 18 Posts

22 February 2013 at 12:56pm

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 %>

Avatar
copernican

Community Member, 189 Posts

23 February 2013 at 2:28am

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 %>

Avatar
Juan

Community Member, 18 Posts

26 February 2013 at 4:06pm

It worked!

thank you OITI!

Highly appreciated!