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

Duplicate template names overwrite each other

 
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
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Mon Jul 04, 2005 10:10 am    Post subject: Duplicate template names overwrite each other Reply with quote

There seems to be a problem when files have identical names.

In the following example there are two PHP files, /dir.php and /subdir/subdir.php, that display two templates, /templates/foo.tpl and /subdir/templates/foo.tpl respectively.

Smarty only uses "foo.tpl" and not the absolute path to each of the files when calculating the compile path, so the two files are written to the same file. The first template to get compiled "wins" and is displayed by both /dir.php and /subdir/subdir.php until the compile_dir is emptied.

/dir.php
Code:
<?php

require_once 'Smarty.class.php';

$smarty = new Smarty();
$smarty->compile_dir = '/var/tmp/smarty-test';
$smarty->display('foo.tpl');

?>


/templates/foo.tpl
Code:
This is foo


/subdir/subdir.php
Code:
<?php

require_once 'Smarty.class.php';

$smarty = new Smarty();
$smarty->compile_dir = '/var/tmp/smarty-test';
$smarty->display('foo.tpl');

?>


/subdir/templates/foo.tpl
Code:
This is subdir/foo


I believe the template file name should be resolved to an absolute path before calculating the compile path.

This also avoids that two compiled templates are generated if the same template is included using different relative names, e.g. {include file="foo.tpl"} and {include file="../templates/foo.tpl"}.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jul 04, 2005 8:10 pm    Post subject: Reply with quote

it's not a bug.

use distinct compile_dirs for different template_dirs or use distinct compile_ids.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Tue Jul 05, 2005 9:42 am    Post subject: Reply with quote

messju wrote:
it's not a bug.

use distinct compile_dirs for different template_dirs or use distinct compile_ids.

Ok. Thanks.

The thing that {include file="foo.tpl"} and {include file="../templates/foo.tpl"} generates two compiled files for the same template is still a minor problem. E.g. when manually clearing the template from the compile cache using $smarty->clear_compiled_tpl("foo.tpl"), you have to call the method once for each path that has been used to refer to the template.

I believe the same problem also applies to is_cached(), clear_cache().

Of course you can choose to use either absolute or relative paths (or call clear_compiles_tpl() twice) in your project, but this quickly gets inconvenient. On Windows, where paths are case-insensitive, you can easily get several versions of the same path if you are not careful (I haven't tested this on Windows, though).

Unless it is an expensive operation, I think getting a canonical representation of the path to the resource before calling _get_auto_filename() may be good idea.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jul 05, 2005 10:40 am    Post subject: Reply with quote

c960657 wrote:
{include file="../templates/foo.tpl"}


you shouldn't use that syntax at all.

Quote:
Unless it is an expensive operation, I think getting a canonical representation of the path to the resource before calling _get_auto_filename() may be good idea.


it *is* an expensive operation.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Tue Jul 05, 2005 10:50 am    Post subject: Reply with quote

messju wrote:
c960657 wrote:
{include file="../templates/foo.tpl"}

you shouldn't use that syntax at all.

Ok. But using an absolute path has the same issues:
{include file="foo.tpl"} vs. {include file="/templates/foo.tpl"}

So does explicitly specifying the resource type:
{include file="foo.tpl"} vs. {include file="file:foo.tpl"} when $smarty->default_resource_type is "file".
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Tue Jul 05, 2005 3:52 pm    Post subject: Reply with quote

c960657 wrote:
Ok. But using an absolute path has the same issues:
{include file="foo.tpl"} vs. {include file="/templates/foo.tpl"}

So does explicitly specifying the resource type:
{include file="foo.tpl"} vs. {include file="file:foo.tpl"} when $smarty->default_resource_type is "file".


It is very difficult to catch and typically not worth it as it is just masking some laziness (read: error prone activity) on the template users part. It is not justification, but note that PHP suffers a similar problem using include_once/require_once.

I think the way to think about it is that the specified resource name is the key -- if multiple keys happen to collide on the same template, that is a problem of the hashing algorithm which, in this case, is you Smile
Back to top
View user's profile Send private message
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