简体   繁体   中英

Smarty foreach loop and file_exist

I'm using the Smarty php template engine , and I'm trying to verify the existence of .jpg images with an $id_attribute name:

{foreach from=$product.colors key='id_attribute' item='color'}
    {if file_exists($img_col_dir|cat:$id_attribute|cat:'.jpg')} 
        yes 
    {else} 
        no 
    {/if}

The $img_col_dir is " http://www.domain.com/img/co/ " and $id_attribute is a number. Many files exists in .jpg format, but the whole loop doesn't find anything, returning "no". What am I doing wrong?

You can't check for existance of a file on a url,

http://www.domain.com/img/co/myfile.jpg

only on your local drive

c:\myfolder\myimages\myfile.jpg

also, you should do this logic in your php file, not in your smarty template.

Check if the file exists then pass through a smarty variable image_exists for each image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM