View previous topic :: View next topic |
Author |
Message |
TylerDurden Smarty n00b
Joined: 24 Jul 2003 Posts: 4
|
Posted: Thu Jul 24, 2003 2:03 pm Post subject: template_exists () problem |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Thu Jul 24, 2003 3:09 pm Post subject: |
|
|
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 |
|
TylerDurden Smarty n00b
Joined: 24 Jul 2003 Posts: 4
|
Posted: Thu Jul 24, 2003 3:35 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jul 24, 2003 5:19 pm Post subject: Re: template_exists () problem |
|
|
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 |
|
TylerDurden Smarty n00b
Joined: 24 Jul 2003 Posts: 4
|
Posted: Thu Jul 24, 2003 7:22 pm Post subject: |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Thu Jul 24, 2003 7:35 pm Post subject: |
|
|
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 |
|
TylerDurden Smarty n00b
Joined: 24 Jul 2003 Posts: 4
|
|
Back to top |
|
|