With a little side of applesauce...

Friday, November 16, 2007

PHP/Smarty - checking if a file exists

I have never worked with smarty templates before, but found this cool tip to allow me to check file_exists("file") within the template itself. I wanted to check to see if a specialmsg.tpl file existed, and display the contents of that file if it existed:

{assign var="specialmsg" value="/location/to/specialmsg.tpl"}
{ if file_exists($specialmsg) }
<td align="right" class="HeadText">&nbsp;</td>
<td valign="top" align="right" class="HeadText">
{include file="specialmsg.tpl"}</td>
<td align="right" class="HeadText">&nbsp;</td>

EDIT: added missing quote as mentioned by CJ.


David said...

Thanks for posting this - exactly what I was looking for! Smarty is an awesome template manager.

CJ said...

Thanx for posting this! You're missing a quotation at the end of the .tpl call. Just wanted to mention that in case someone copied/pasted directly.

Shannon Eric Peevey said...

Thanks, CJ!

Anonymous said...

Great! thanks

Anonymous said...

2 days i'm looking for this. I cannot find in Smarty, at least i found here. Great thank you... Super!
You make my life easier..

Anonymous said...

its not working for

Anonymous said...

it does not work for links, but lokal paths, so use "path/to/avatar.jpg" instead