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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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?
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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}

User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3483
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

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

Post 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.
Post Reply

Return to “CMSMS Core”