smarty模板新手有关问题:提示在模板中发现未定义函数

php中文网
发布: 2016-06-13 13:33:36
原创
1038人浏览过

smarty模板新手问题:提示在模板中发现未定义函数
模板代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->{include file ='header.tpl'}
<form name="oform" action="index.php" method="post" onsubmit="">
     <table border="1" align="center" width="70%" cellpadding="3" cellspacing="0">   
        <caption><h3>{$tableName}<h3></caption>    
          <tr> 
              <td width="20%">姓名:</td><td><input type="string" name="ceshi" {if $wandoujia} value='{$wandoujia}'{/if} /></td>
          </tr>
          <tr>
              <td>测试:</td><td><input type="string" name="ceshi2" /></td>
          </tr>  
          <tr><td colspan="2"><input type="submit" value="查询" /></td></tr>
  </table> 
</form>
{include file ='footer.tpl'}
登录后复制


index.php调用代码:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php
require_once dirname(__FILE__) . "\include\common.inc.php";
require_once SR_TEMPLATE . "\TemplatesFunction.php";
     $smarty = $GLOBALS['smarty'];
     $smarty -> assign('title','测试标题'); 
     $smarty -> assign('Version',"2011"); 
     $smarty -> assign('HomePageLink','http://localhost/Matance1.0/'); 
     $smarty -> assign('tableName','标题'); 
    

    
    if(isset($_POST['ceshi2'])){
          $smarty -> assign('wandoujia',$_POST['ceshi2']); 
     }
    $smarty -> display('index.tpl');
?>
登录后复制


即在index.tpl模板上作一表单,action="index.php",index.php在加载模板时判断有木有传值,如果有,则给模板中$wandoujia函数传值

但运行后,总是提示:

Notice: Undefined index: wandoujia in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
Notice: Trying to get property of non-object in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
/>

求教

------解决方案--------------------
第一,这个不是未定义函数,只是为定义的index,
第二,看看此文件34行即可
D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php
------解决方案--------------------
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']); 
}

没考虑wandoujia没有分配的情况,如果wandoujia没有值自然或报错。
------解决方案--------------------
把错误级别号改为5或者7,只报打错,Notice错误可以直接忽略,对站点运行豪无影响,我们自己早期开发的PHP站点,因为当时写得不规范,至少有上百个NOTICE级别错误,但是依然完美运行几年了。现在大家也懒得去修正。无所谓。
------解决方案--------------------
如果一定要纠正此NOTICE级别的问题,你可以这么做:除了在PHP代码里判断POST提交外:
 if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']); 
}
还应该在模板里用SMARTY也判断是否有POST提交。如果有就加入SMARTY变量wandoujia。注意这一段:
 
姓名:

试着改为:
  
姓名:

这里不一定是这样写,你最好直接看看POST提交值的模板的2种输出情况,{$smarty.post.ceshi2}直接插入到一个标签里显示一下。看看有提交值跟没提交值适合{$smarty.post.ceshi2}的输出差别。这样就能判断是否需要加入$wandoujia变量了。
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号