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

致命错误:未捕获 - > 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 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真