View previous topic :: View next topic |
Author |
Message |
nightsky Smarty Rookie
Joined: 16 Aug 2005 Posts: 18
|
Posted: Tue Aug 16, 2005 11:24 pm Post subject: How to use rel path: template_dir = '../smarty/templates/'? |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Aug 17, 2005 4:07 am Post subject: |
|
|
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 |
|
nightsky Smarty Rookie
Joined: 16 Aug 2005 Posts: 18
|
Posted: Thu Aug 18, 2005 8:26 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Aug 18, 2005 8:30 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Aug 18, 2005 8:34 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Aug 18, 2005 8:39 pm Post subject: |
|
|
Test if a normal PHP include works with a relative path:
include ('../smarty/templates/test.php'); |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Aug 18, 2005 9:58 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Aug 18, 2005 10:03 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Aug 18, 2005 10:10 pm Post subject: |
|
|
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 |
|
markwu Smarty Rookie
Joined: 28 Sep 2005 Posts: 6
|
Posted: Wed Feb 01, 2006 6:46 pm Post subject: |
|
|
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 |
|
markwu Smarty Rookie
Joined: 28 Sep 2005 Posts: 6
|
Posted: Wed Feb 01, 2006 7:57 pm Post subject: |
|
|
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 |
|
|