Page 1 of 1

if file exists show it, if not don't show anything

Posted: Thu May 23, 2019 3:34 pm
by Sendlingur
I'm using this snippet to display file in a div if it exist.

Code: Select all

 
{if file_exists($entry->fields.Image2)} 
 <div class="col-md-6"> 
<img src="{$entry->file_location}/{$fields.Image2->displayvalue}{$entry->fields.Image2->displayvalue}" class="img-responsive">
</div>
{/if}
if I write it this way the image is not displaying.

if I write it with " !file_exists " the image is showing.

I have also written it like "{if file_exists($entry->fields.Image2)== false}"

but it gives the same result

Some of my posts have an image in this custom field and others don't.
those how don't have it always shows a broken image.

Is there another way to do this kind of operation or am I misunderstanding something?

Re: if file exists show it, if not don't show anything

Posted: Thu May 23, 2019 4:06 pm
by Sendlingur
Ok I figured this Out.

I made it work by doing the following:

Code: Select all

{capture name="cust_img"}{$entry->fields.Image2->displayvalue}{/capture}
{if empty($smarty.capture.cust_img)}
 
 {else}
 <div class="col-md-6"> 
 <img src="{$entry->file_location}/{$fields.Image2->displayvalue}{$entry->fields.Image2->displayvalue}" class="img-responsive">
 </div>
{/if}


Re: if file exists show it, if not don't show anything

Posted: Fri May 24, 2019 10:54 am
by velden
Without testing I'd rewrite to:

Code: Select all

{if !empty($entry->fields.Image2->displayvalue)}
 <div class="col-md-6"> 
 <img src="{$entry->file_location}/{$fields.Image2->displayvalue}{$entry->fields.Image2->displayvalue}" class="img-responsive">
 </div>
{/if}
Capture is said to be inefficient. The empty statement block is not nice.