Re: [SMARTY] Directories
| From: | Ferdinand Beyer | Date: | Sun, 09 Feb 2003 09:54:23 +0000 |
| Subject: | Re: [SMARTY] Directories | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-7603@lists.php.net to get a copy of this message | ||
On 8 Feb 2003 at 23:32, Felix wrote:
>
> Hello!
> I've just installed smarty into the directories www.domain.com/include/,
> www.domain.com/include/templates/, www.domain.com/include/templates_c/ etc. !
> I took the example file that is included in the smarty.zip. It works fine. But if I move it to
> www.domain.com/ it doesn't work anymore. I changed the smarty.class.php include path to
> include/smarty.class.php but it isn't able to read the template file.
> If I create a new folder www.domain.com/include/test/ and move index.php into this directory it
> works. Don't ask me why or how! But if I create a further directory, let's take
> www.domain.com/include/test/php/ for example, it doesn't work anymore. Do I have to change the
> compile path, cache path
and the template path everytime I create a file in different folders? If this is the case, how do I
have to?
> Do I need the document root to change the path like $document_root/include/templates or like
> that?
> Thanks in advance!
>
> Felix!
Could you please set error_reporting to E_ALL and tell us what kind
of error messages you get from Smarty?
If I undestood you right, you want to include Smarty.class.php from
PHP files in different folders and Smarty doesn't find the templates?
This could happen since by default all Smarty dirs are relative and
expected relative to the CWD ("Current Working Directory"; usually
the directory of the PHP file called from outside).
I recommend to use absolute paths since that way it doen't matter
what the CWD is. But I do not recommend to apply Smarty's settings
directly in the Smarty class, here are the reasons:
1. Every time you want to upgrade Smarty, you'll have to change the
settings in the Smarty class file again.
2. You cannot use "dynamic code" within the property declaration,
eg. it is not allowed to code
var $template_dir = realpath('../templates');
Instead, I recommend to hold the settings central by one of the
following ways:
1. Use an init script within the smarty directory that is included by
every script that wants to use Smarty:
define('SMARTY_DIR', dirname(__FILE__) .
DIRECTORY_SEPARATOR);
require_once SMARTY_DIR . 'Smarty.class.php';
$smarty = new Smarty();
$smarty->template_dir = ...
(...)
2. Use a factory function for Smarty:
function get_smarty()
{
$smarty = new Smarty();
$smarty->template_dir = ...
(...)
}
3. Use a custom Smarty class. This is the way I usually do it as you
can even extend Smarty with your own methods.
class AdaptedSmarty extends Smarty
{
function AdaptedSmarty()
{
// In the constructor you can set Smarty's properties as you
want
$this->template_dir = ...
(...)
}
}
By using one of the methods above you can use absolute paths for
Smarty's directories by either hard-code them or calculate them
dynamically (e.g. using __FILE__, SMARTY_DIR, getcwd(),
dirname(), realpath(), ...).
HTH
--
Ferdinand Beyer
<fbeyer@clickhand.de>