首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么智能模板引擎找不到我的模板文件?

为什么智能模板引擎找不到我的模板文件?
EN

Stack Overflow用户
提问于 2014-12-29 18:39:23
回答 1查看 2K关注 0票数 2

为什么智能模板引擎找不到我的模板文件?

致命错误: Uncaught > Smarty:无法在第129行的C:\xampp\htdocs\testing\includes\smarty\sysplugins\smarty_internal_templatebase.php中加载模板文件'test.tpl‘<-

$smarty->testInstall();也做了

代码语言:javascript
复制
Smarty Installation test...
Testing template directory...
C:\xampp\htdocs\testing\templates\frontend\default\tpl is OK.
Testing compile directory...
C:\xampp\htdocs\testing\templates_c\frontend is OK.
Testing plugins directory...
C:\xampp\htdocs\testing\includes\smarty\plugins is OK.
Testing cache directory...
C:\xampp\htdocs\testing\cache is OK.
Testing configs directory...
C:\xampp\htdocs\testing\configs is OK.
Testing sysplugin files...
... OK
Testing plugin files...
... OK
Tests complete.

var_dump($smarty->getTemplateDir());

数组(1){ "C:/xampp/htdocs/testing/templates/frontend/default/tpl\“字符串(55) => }

文件模式

代码语言:javascript
复制
htdocs
    -- testing
        -- incluses
            -- smarty
                plugins
                sysplugins
                Smarty.class.php
                SmartyBC.class.php
            -- configs
                configs.php
            -- db
                connect.php
                db.php
        -- configs
        -- cache
        -- templates
            -- frontend
                -- default
                    -- css
                    -- mages
                    -- js
                    -- tpl
                        test.tpl
            -- backend
        -- templates_c
            -- frontend
        index.php

index.php

代码语言:javascript
复制
<?php

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('display_startup_errors', TRUE);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);

ob_start();
session_start();

require 'includes/smarty/Smarty.class.php';
require 'includes/db/db.php';
require 'includes/configs/configs.php';

$page = isset($_GET['do']) ? $_GET['do'] : '';

switch($page){

    case 'home';
    include 'pages/home.php';
    break;

    default:
    include 'pages/test.php';
    break;

}

ob_flush();

?>

configs.php

代码语言:javascript
复制
<?php

$smarty = new Smarty();
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->cache = 1;
$smarty->setTemplateDir('C:/xampp/htdocs/testing/templates/frontend/default/tpl');
$smarty->setCompileDir('C:/xampp/htdocs/testing/templates_c/frontend/default');
$smarty->setCacheDir('C:/xampp/htdocs/testing/cache');
$smarty->setConfigDir('C:/xampp/htdocs/testing/configs'); 

?>

test.php

代码语言:javascript
复制
<?php

//$success = 'Success Message';
//$error = 'Error Message';
$errors[] = 'Error one';
$errors[] = 'Error two';

$smarty = new Smarty;
//$smarty->assign('success', $success);
//$smarty->assign('error', $error);
$smarty->assign('errors', $errors);
$smarty->display('test.tpl');

?>

test.tpl

代码语言:javascript
复制
{if !empty($errors)}
<div id="errors">
{section name=i loop=$errors}
{$errors[i]}<br />
{/section}
</div>
{/if}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-29 20:28:48

尽管调试这一点令人困惑,但实际上是全局变量$smarty的一个简单问题,它最初是在configs.php中设置的Smarty对象,在包含test.php时再次被覆盖。Smarty失去了其配置的$template_dir,因为对象$smarty被重新初始化为默认值。模板的默认位置是在./templates中查找,这就是它使用C:/xampp/htdocs/testing/templates/test.tpl的原因。

解决方案:只是不要在$smarty = new Smarty();中执行test.php

test.php

代码语言:javascript
复制
<?php

//$success = 'Success Message';
//$error = 'Error Message';
$errors[] = 'Error one';
$errors[] = 'Error two';

// Don't do this:  $smarty is already initialized and configured.
// $smarty = new Smarty;

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

?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27694196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档