简体   繁体   中英

Fatal error: Uncaught --> Smarty: Unable to load template 'file: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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM