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

How to use rel path: template_dir = '../smarty/templates/'?

 
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
nightsky
Smarty Rookie


Joined: 16 Aug 2005
Posts: 18

PostPosted: Tue Aug 16, 2005 11:24 pm    Post subject: How to use rel path: template_dir = '../smarty/templates/'? Reply with quote

Is there a way to get relative paths to work, e.g.

$this->template_dir = '../smarty/templates/';

It works with '.' to indicate current directory, shouldn't it also work with '..' to indicate same level but outside (ie 'next to') document root? Especially since this is the recommended setup -- templates outside the doc root.

It also doesn't work with using a constant in the path, so with SMARTY_DIR defined as '../smarty', this chokes:

$this->template_dir = SMARTY_DIR . 'templates/';

I can only get it to work with a full machine path:

$this->template_dir ="/clients/mycompany/http/smarty/templates/";

Is the ../ syntax officially unsupported? i couldn't find any mention of this anywhere.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Aug 17, 2005 4:07 am    Post subject: Reply with quote

Relative paths do work, although they are relative to the executing script, not docroot. Relative paths are slower performance than absolute paths, and typically result in problems. That is why absolute are recommended.
Back to top
View user's profile Send private message Visit poster's website
nightsky
Smarty Rookie


Joined: 16 Aug 2005
Posts: 18

PostPosted: Thu Aug 18, 2005 8:26 pm    Post subject: Reply with quote

Monte, thanks for the reply.

It doesn't work when I use a path relative to the script, eg say I place scripts and templates in the following directories

/usr/local/myclient/htdocs/index.php
/usr/local/myclient/smarty/templates/

the script index.php chokes if I define the template directory as

../smarty/templates/

even though that's where it is relative to the currently executing script. If I move it within the document root, and change it to ./smarty/templates, it works fine. It seems to only be a problem when i use the .. syntax.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Aug 18, 2005 8:30 pm    Post subject: Reply with quote

I would think it would work, unless there is a permission problem accessing files outside of your document root. Does the absolute path /usr/local/myclient/smarty/templates/ work ?
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: Thu Aug 18, 2005 8:34 pm    Post subject: Reply with quote

Like Monte said, relative paths are a pain.

Get your scripts to localize themselves to avoid these issues. For example, create a config.php in your webroot that does something like:

define('ROOT_PATH', dirname(__FILE__));

Then you have the constant ROOT_PATH available as the absolute path to your webroot. From there, it is quite simple to build other absolute paths as needed. Simply call the config.php file or better, make it a prepend script.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Aug 18, 2005 8:39 pm    Post subject: Reply with quote

Test if a normal PHP include works with a relative path:

include ('../smarty/templates/test.php');
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: Thu Aug 18, 2005 9:58 pm    Post subject: Reply with quote

AFAIK smarty doesn't look for templates in php's include_path. It just looks for $smarty->template_dir inside include_path if $smarty->template_dir is a relative path.

setting $smarty->template_dir = '.' may make this work.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


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

PostPosted: Thu Aug 18, 2005 10:03 pm    Post subject: Reply with quote

You can also try putting "." in your php include_path. Or, stick to absolute paths. No sense in making PHP do the include_path traversal on every invocation.
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: Thu Aug 18, 2005 10:10 pm    Post subject: Reply with quote

ah, sorry, I missed the initial question - forget my latest comment.

on systems where not every single disk hit and not every single cpu cycle is crititcal, i often use something like this (in a config.php):

[php:1:8b4a9ad000]define('BASEDIR', realpath(dirname(__FILE__)) . '/');
$smarty->template_dir = BASEDIR . 'templates';[/php:1:8b4a9ad000]

this makes the whole thing relocatable between servers but still gives smarty absolute paths (which is always safest/best).
Back to top
View user's profile Send private message Send e-mail Visit poster's website
markwu
Smarty Rookie


Joined: 28 Sep 2005
Posts: 6

PostPosted: Wed Feb 01, 2006 6:46 pm    Post subject: Reply with quote

messju wrote:
ah, sorry, I missed the initial question - forget my latest comment.

on systems where not every single disk hit and not every single cpu cycle is crititcal, i often use something like this (in a config.php):

[php:1:d4a09f9640]define('BASEDIR', realpath(dirname(__FILE__)) . '/');
$smarty->template_dir = BASEDIR . 'templates';[/php:1:d4a09f9640]

this makes the whole thing relocatable between servers but still gives smarty absolute paths (which is always safest/best).


Hi messju:

I meet the same problem, and I try to patch the the smarty.class.php to fix this problem, it seems work.

I change the code (From 1540-1543, in 2.6.12)

[php:1:d4a09f9640]
if (isset($params['resource_base_path']))
$_params['resource_base_path'] = $params['resource_base_path'];
else
$_params['resource_base_path'] = $this->template_dir;
[/php:1:d4a09f9640]

to

[php:1:d4a09f9640]
if (isset($params['resource_base_path']))
{
$_params['resource_base_path'] = (array)$params['resource_base_path'];
$_params['resource_base_path'][] = ".";
}
else
{
$_params['resource_base_path'] = (array)$this->template_dir;
$_params['resource_base_path'][] = ".";
}
[/php:1:d4a09f9640]

Please kindly let me know does there any draw backs if I change the code like this?

Regards, Mark
Back to top
View user's profile Send private message
markwu
Smarty Rookie


Joined: 28 Sep 2005
Posts: 6

PostPosted: Wed Feb 01, 2006 7:57 pm    Post subject: Reply with quote

Forgot my previous question, I think I can add a "." to the $template_dir, it can also solve the problem, and won't change any smarty core.

Regards, Mark
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 -> 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