Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

template_exists () problem

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
TylerDurden
Smarty n00b


Joined: 24 Jul 2003
Posts: 4

PostPosted: Thu Jul 24, 2003 2:03 pm    Post subject: template_exists () problem Reply with quote

Hi all,

I have a problem with the template_exists () function. Im trying to use it in an if statement to check whether a template file exists before trying to parse it to avoid getting error-messages.

My syntax:

Code:
{if template_exists("myname.tpl")}
...
{/if}


However I get a "Call to undefined function: template_exists()" error. What am I doing wrong?

Later I want to use something like:

Code:
{if template_exists(`$values[i].category`.tpl)}


What would be the correct syntax for this?
Thanks in advance.
_________________
http://www.titleofsite.com
Back to top
View user's profile Send private message Visit poster's website AIM Address
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Thu Jul 24, 2003 3:09 pm    Post subject: Reply with quote

template_exists is ment to be used outside a template in a PHP file like $smarty->template_exists('blah.tpl'); you could if you wanted to put [php:1:a8bad71318]function template_exists($tpl) {
global $smarty;
return $smarty->template_exists($tpl);
}[/php:1:a8bad71318] in your calling PHP file and make sure to change $smarty to whatever var you called to hold the class and that would work.
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
TylerDurden
Smarty n00b


Joined: 24 Jul 2003
Posts: 4

PostPosted: Thu Jul 24, 2003 3:35 pm    Post subject: Reply with quote

Brilliant, that worked.
Inside my template I'm now calling:
Code:
{if template_exists("`$values[i].category`.tpl")}

Works as intended.
Thanks.
_________________
http://www.titleofsite.com
Back to top
View user's profile Send private message Visit poster's website AIM Address
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 24, 2003 5:19 pm    Post subject: Re: template_exists () problem Reply with quote

TylerDurden wrote:
I have a problem with the template_exists () function. Im trying to use it in an if statement to check whether a template file exists before trying to parse it to avoid getting error-messages.


Tyler, what do you mean by that? Smarty already checks for template existence before it compiles the template. If you don't want the error mesage, you can write an error handler to catch the error. You may also consider writing a Smarty class wrapper that overrides the existing display() and fetch() but include your checking algorithm before delegating to the native methods. I'm not sure what you intend to do in the case that the template doesn't exisit, though. Is that something you want in the users control or do you have a policy for that?
Back to top
View user's profile Send private message
TylerDurden
Smarty n00b


Joined: 24 Jul 2003
Posts: 4

PostPosted: Thu Jul 24, 2003 7:22 pm    Post subject: Reply with quote

Thanks, but AZTEK already provided an answer. If the template doesn't exist it simply isn't parsed. I use this for embedding some static html-pages. However, for future projects, what smarty functions/variables could I use to catch smarty errors for writing, as you suggest, an error-handler.

Thanks.
_________________
http://www.titleofsite.com
Back to top
View user's profile Send private message Visit poster's website AIM Address
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Thu Jul 24, 2003 7:35 pm    Post subject: Reply with quote

http://www.php.net/manual/en/function.set-error-handler.php allows you to send PHP and Smarty (and since Smarty uses trigger_error) errors to a custom error function for logging/display.
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
TylerDurden
Smarty n00b


Joined: 24 Jul 2003
Posts: 4

PostPosted: Thu Jul 24, 2003 7:47 pm    Post subject: Reply with quote

Thanks, I'll look into that.
_________________
http://www.titleofsite.com
Back to top
View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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