Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Check if a file exists "$tpl_dir./test.html"

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
Jose
Smarty n00b


Joined: 08 Sep 2017
Posts: 2

PostPosted: Fri Sep 08, 2017 4:25 pm    Post subject: Check if a file exists "$tpl_dir./test.html" Reply with quote

Good afternoon, I'm trying to include a html file in a section inside the product sheet.

For this I have included in the product.tpl this code:

Code:
{include file="$tpl_dir./test.html"}



In this case it is a test, but I want the file name to be the Product Code.html. It also works fine, but if that file does not exist, it gives an error.

In php it is simple to check if that url or file exists. But I do not know how to do it on smarty.

I've tried introducing php code between tags {php} {/ php}, but it does not work, as I've seen it is obsolete.

You could tell me some way, to check the availability of that file, to put it in an if, that only enters when it exists ...

Thank you very much for your help. I've been behind this for many hours and I do not know where to go.
Thank you!!
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 195
Location: West Coast

PostPosted: Sat Sep 09, 2017 3:59 am    Post subject: Reply with quote

I would try:
Code:
{if file_exists("$tpl_dir/test.html")}
{include file="$tpl_dir/test.html"}
{else}
{include file="$tpl_dir/404.html"}
{/if}

Many PHP functions are usable in Smarty.
Back to top
View user's profile Send private message
Jose
Smarty n00b


Joined: 08 Sep 2017
Posts: 2

PostPosted: Sat Sep 09, 2017 6:27 am    Post subject: Thanks Reply with quote

Thank you very much, it works perfectly. It was much simpler than I thought. Thanks again, you take away a big problem from above !!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1389

PostPosted: Mon Sep 11, 2017 3:12 pm    Post subject: Re: Check if a file exists "$tpl_dir./test.html" Reply with quote

Jose wrote:
but if that file does not exist, it gives an error.
Of course it gives an error. Because you made it - you told it to include a nonexistent file. And it is your responsibility to fix it. Not to mask it with senseless workarounds.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP