doupixian1436 2018-07-12 08:02
浏览 432
已采纳

致命错误:未捕获 - > Smarty:无法加载模板'文件:test.tpl'

I have a smarty-test02 project, in the php/test.php file:

<?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');

and its template is 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>

but when I access the test.php by http://localhost:63342/smarty-test02/php/test.php I get bellow error:

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

In my 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

The directory structure:

$ 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条回答 默认 最新

  • drde3456 2018-07-12 08:58
    关注

    Use absolute path to template directory:

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

    it worked for me (with your scripts)

    PHP understood your ./ directory as test/ directory, because you run php/test.php file (and include smartyHeader.php in him, but it still php/ directory - NOT root), therefore PHP tried to find php/templates/test.tpl file, but the one isn't exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料