致命错误:未捕获->聪明:无法加载模板'file:test.tpl'

时间:2018-07-12 08:02:59

标签: php smarty

我在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>

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

  

致命错误:没捉到->聪明:无法加载模板'file:test.tpl'<-在/Users/sof/Desktop/TestPHP/smarty-test02/libs/sysplugins/smarty_internal_template.php中抛出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�\210pfung.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

1 个答案:

答案 0 :(得分:0)

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

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

它为我工作(使用您的脚本)

PHP将./目录理解为test/目录,因为您运行php/test.php文件(并在其中包含smartyHeader.php,但仍然是php/目录- 不是根),因此PHP试图找到php/templates/test.tpl文件,但该文件不存在。