cakephp placeholder if image not exist
-
05-07-2021 - |
Question
I have a database with images associated to each entry.
Anyhow, some entries don't have an image.
Googling I found some js and php way to apply in the view, but I'm pretty sure there is a (much nicer) MVC-way to do this, just I don't know how..
Is there a nice Cake-way to display placeholder image where none exist?
Solution
Supposed the data you set from the controller named 'post'. And you have 'your_default_image' in your /app/webroot/img/
<?php
if(empty($post['Post']['img'])) {
echo $this->Html->image($post['Post']['img']);
} else {
echo $this->Html->image('your_default_image');
}
And you can wrap those logic in your helper.
OTHER TIPS
You can use placehold.it anywhere you need for placeholding images.
Just use http://placehold.it/WxH like http://placehold.it/250x250 and you're set.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow