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

Bug in smarty_resource.php, line 313

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
adebruyn
Smarty n00b


Joined: 29 Oct 2011
Posts: 3

PostPosted: Sat Oct 29, 2011 2:22 pm    Post subject: Bug in smarty_resource.php, line 313 Reply with quote

Hello:

Here is my PHP code. Pretty straightforward, in a test.php file.

...
$smarty = new Smarty;
$smarty->display('file:test.tpl');

test.php and test.tpl are both in the same directory.

When I run this script with a custom error handler (set_error_handler(...)), there is an error triggered in smarty_resource.php, line 313.

filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for .\templates\test.tpl

I've tested the value of $file in the fileExists(Smarty_Template_Source $source, $file) function. Smarty test the following template locations, sequentially:

.\templates\test.tpl (not found)
test.tpl (correct)

And not finding the first one, throw an error message (E_WARNING). I can't allow systematic E_WARNING messages in my application. Corporate policy.

Now what ? Is it a "bug", or did I do somethig wrong that makes Smarty (v3.1.4) test a file that does not exist ?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Oct 29, 2011 3:06 pm    Post subject: Reply with quote

smarty should only check template directories for templates. ie. the current directory will not be tested unless added to the template_dir array. not finding test.tpl in the template_dir array is an expected warning.

you could also employ a default template handler to catch the case when a template is not found and do something with it instead of a warning.
Back to top
View user's profile Send private message Visit poster's website
adebruyn
Smarty n00b


Joined: 29 Oct 2011
Posts: 3

PostPosted: Sun Oct 30, 2011 12:07 pm    Post subject: Reply with quote

Thanks mohrt.

First of all, your answer does not seem to be correct. After running some tests, whether you specifically include the current directory through setTemplateDir or not, it will always be searched, no matter what. So these two seem to be perfectly equivalent on my end:

$smarty->setTemplateDir(array('.', '/mytemplates'));
$smarty->setTemplateDir('/mytemplates');

Second, it doesn't solve my problem. Say my templates can be found in multiple directories (general header and footer in a global directory, and template specific for the current page in current directory), and I set:

$smarty->setTemplateDir(array('.', '/mytemplates'));

Now, if I use 'mytemplates/footer.tpl', smarty will still issue a warning when it doesn't find ./footer.tpl. That's kind of unexpected...

And again, I can't allow systematic warnings in my application, error handling are very strict.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Oct 30, 2011 1:33 pm    Post subject: Reply with quote

when you set the template_dir to '/templates' that is a system root directory. If you want it relative to the cwd you want './templates'. is that part of the problem? I'll investigate that automated '.' issue.
Back to top
View user's profile Send private message Visit poster's website
adebruyn
Smarty n00b


Joined: 29 Oct 2011
Posts: 3

PostPosted: Sun Oct 30, 2011 1:48 pm    Post subject: Reply with quote

No, what I have is fine. The recurring templates (header, footer, etc.) are in the root directory, as expected, and they are found.

My problem is not so much that smarty does not find the templates --it does, as expected, despite the automated '.' issue we talked about--.

My problem is that it generates a warning everytime the file is not in one of the listed directories it is supposed to search. So if I list 5 directories (silly, but...), and the template I need is in the last one of them, I get 4 warnings. If it's in the first, I get no warning.

I think it's a weird behavior that might require investigation. After all, if I list multiple directories, Smarty should only trigger one error at the end if the template cannot be found anywhere. Triggering a warning at each search step does not make much sense to me...

By the way, this problem only popped up because I have my own set_error_handler.

Thanks again for your help.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Oct 30, 2011 1:54 pm    Post subject: Reply with quote

that doesn't sound right, it should emit a warning only if the template is not found anywhere. will look into it.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Oct 30, 2011 2:02 pm    Post subject: Reply with quote

See www.smarty.net/forums/viewtopic.php?t=20204

See explaination by rodneyrehm


Your cutom error handlers could also check if the error was caused by a statement with @ error suppression. See example in the last post.


Last edited by U.Tews on Sun Oct 30, 2011 2:13 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Oct 30, 2011 2:12 pm    Post subject: Reply with quote

ah yeah. we do a @filemtime() on each file/directory, and that will throw a warning for files no found, which is suppressed. we don't run file_exists() first for both speed and race condition issues. for a custom error handler, you must test if error_reporting() === 0 to correctly handle @suppressed errors.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Oct 30, 2011 2:27 pm    Post subject: Reply with quote

and there is no automated '.' search. could you provide a simple test if you see otherwise?
Back to top
View user's profile Send private message Visit poster's website
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 -> Bugs 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