Smarty中的自定义功能

时间:2012-08-12 05:44:55

标签: smarty

我想创建自定义智能功能。我创造了功能     

 function smarty_function_vbchecker($params, &$smarty)
{
    $length = strlen($params['vbstring']);
    $output = "Your sentence is too long. Shorten It!";
    if($length < 50)
        $output = "Your sentence is just right!";

    return $output;
}
?>

我已将文件保存在smarty / libs / plugins目录中。之后我创建了模板文件testfunction.tpl

{vbchecker vbstring='See, it is very easy to write a function.'}

我已将文件保存在smarty / templates目录中

我还创建了文件testfun.php,我打电话来查看输出。

<?php
require 'Smarty/libs/Smarty.class.php';

$smarty = new Smarty;


    $smarty->display('tesfunction.tpl');
    ?>

但是当我运行testfun.php时,我看到错误:

Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template file 'tesfunction.tpl'' in D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatebase.php:127 Stack trace: #0 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatebase.php(374): Smarty_Internal_TemplateBase->fetch('tesfunction.tpl', NULL, NULL, NULL, true) #1 D:\xampp\htdocs\smarty\testfun.php(7): Smarty_Internal_TemplateBase->display('tesfunction.tpl') #2 {main} thrown in D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatebase.php on line 127

这是什么原因。我该怎么做才能看到输出。

Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template ".\templates\testfunction.tpl" on line 9 "{vbchecker vbstring='See, it is very easy to write a function.'}" unknown tag "vbchecker"' in D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatecompilerbase.php:656 Stack trace: #0 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatecompilerbase.php(441): Smarty_Internal_TemplateCompilerBase->trigger_template_error('unknown tag "vb...', 9) #1 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templateparser.php(2393): Smarty_Internal_TemplateCompilerBase->compileTag('vbchecker', Array) #2 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templateparser.php(3096): Smarty_Internal_Templateparser->yy_r36() #3 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templateparser.php(3196): Smarty_Internal_Templateparser->yy_reduce(36) #4 D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_smartytemplatecompiler. in D:\xampp\htdocs\smarty\smarty\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 656

1 个答案:

答案 0 :(得分:0)

例如,您还可以在php中访问静态方法:

class Foo {
   public static function boo($var) {
   // Your code
      return $var;
   }
}

聪明地打电话:

{Foo::boo('my text')}

如果使用名称空间,请添加它。我不再使用智能功能了。