Fatal error: Uncaught --> Smarty: Unable to load template 'file:test.tpl'

Fatal error: Uncaught --> Smarty: Unable to load template 'file:test.tpl'

我有一个 smarty-test02 项目,在 php/test.php 文件中:

<?php

require($_SERVER['DOCUMENT_ROOT'] . '/smartyHeader.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/test/test01.php');

$msg = 'hello world, this is my first Smarty!';
$title = 'Smarty Title';

$smarty->assign('title', $title);
$smarty->assign('message', $msg);

$movies_arr = array('A'=>'a film', 'B' => 'b file', 'C' => 'c film');

$smarty->assign('movie_arr', $movies_arr);

$smarty->assign('v', ['a', 'b', 'c']);

$extraTemplateVariables = array();

$extraTemplateVariables['test_list'] = ['a', 'b', 'c', 'd'];
$extraTemplateVariables['selected_product'] = '';  

$smarty->assign('extraTemplateVariables', $extraTemplateVariables);

$smarty->display('test.tpl');

它的模板是 templates/test.tpl :

<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        {*{$title}*}
    </body>
{literal}
    <script lang="javascript">
        function fun(){
            alert('asd');// there I want to alert the $title)
        }
        fun();
    </script>
{/literal}

</html>

但是当我通过 http://localhost:63342/smarty-test02/php/test.php 访问 test.php 时,出现以下错误:

Fatal error: Uncaught --> Smarty: Unable to load template 'file:test.tpl' <-- thrown in /Users/sof/Desktop/TestPHP/smarty-test02/libs/sysplugins/smarty_internal_template.php on line 187


EDIT-1

在我的 smartyHeaders.php:

<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/libs/Smarty.class.php');

$smarty = new Smarty();
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->template_dir = './templates';
$smarty->compile_dir = './templates_c';

EDIT-2

目录结构:

$ tree .
.
├── addon
│   └── test01_addon.php
├── cache
│   └── a521c2377356a0c5c1792bcb5adcde857b3c48e3.test.tpl.php
├── composer.phar
├── libs
│   ├── Autoloader.php
│   ├── Smarty.class.php
│   ├── SmartyBC.class.php
│   ├── bootstrap.php
│   ├── debug.tpl
│   ├── libs\ -\ Verknu�0pfung.lnk
│   ├── plugins
│   │   ├── block.textformat.php
      ...
│       ├── smarty_variable.php
│       ├── smartycompilerexception.php
│       └── smartyexception.php
├── php
│   └── test.php
├── smartyHeader.php
├── templates
│   ├── child.tpl
│   ├── parent.tpl
│   ├── php
│   │   └── test.tpl
│   ├── test01.tpl
│   └── test02.tpl
├── templates_c
│   ├── 3963a63f17ac1e915beafe6a28decb3ece4b8a7a_0.file.test01.tpl.cache.php
└── test
    └── test01.php

使用模板目录的绝对路径:

$smarty->template_dir = $_SERVER['DOCUMENT_ROOT'].'/templates';
$smarty->compile_dir = $_SERVER['DOCUMENT_ROOT'].'/templates_c';

它对我有用(用你的脚本)

PHP把你的./目录理解为test/目录,因为你运行php/test.php文件(并且在他里面包含了smartyHeader.php,但它仍然是 php/ 目录 - NOT 根),因此 PHP 试图找到 php/templates/test.tpl 文件,但那个文件不存在。