...

View Full Version : Smarty {if file_exists}



jcx1028
04-17-2012, 07:22 PM
Hi there. I've been working with Flynax's auto classifieds script, which is the first I've ever seen or used Smarty. And honestly, I'm not the best with regular PHP to begin with. So please pardon me while I ask a potentially dumb question.

On the dealer profile pages of our site (http://www.theusedcarplace.com/index.php?page=dealer-accounts&id=24), I'm using the following code to pull in a JPG of the dealer's most recent print ads.


<div class="print-ads">
<a href="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf" target="_blank"><img src="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
<p>Click to View PDF</p>
</a>
</div>

The problem is, on pages for dealers who haven't run a print ad (http://www.theusedcarplace.com/index.php?page=dealer-accounts&id=4), there obviously isn't a corresponding JPG, so I get a missing image.

If possible, I'd like to use an {if} statement to show the JPG if it exists, but to show nothing if it does not. Here's what I've been playing with...


{assign var="printad" value="/print_ads/{$account.Full_name|replace:" ":"_"|lower}.jpg"}
{if file_exists($printad)}
<div class="print-ads">
<a href="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf" target="_blank"><img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
<p>Click to View PDF</p>
</a>
</div>
{/if}

...Except that doesn't work. It pretty much breaks the entire site. Could that be because of the nested brackets in the first line? Because the following doesn't really work either, but at least it doesn't result in a white page...


{assign var="printad" value='/print-ads/[$account.Full_name|replace:" ":"_"|lower].pdf'}
{if file_exists($printad)}
<img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
{else}
0
{/if}

Any suggestions? Please keep in mind that I'm a coding idiot.

angst
04-17-2012, 07:51 PM
this isn't php. try the smarty forum: http://www.smarty.net/forums/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum