我在创建智能函数插件时遇到了一个小问题。代码如下。
<?php
function smarty_function_selectdate(array $params, Smarty_Template_Instance) {
//year to start with
$startdate = 1960;
//year to end with - this is set to current year. You can change to specific year
$enddate = date("Y");
$years = range ($startdate,$enddate);
//print years
foreach($years as $year)
{
echo "<option value='".$year."'>".$year . "</option>";
}
}
?>
虽然PHP代码应该可以工作,但当我把它放在TPL文件中时,如下所示。我得到这个奇怪的错误
{selectdate}
错误
Parse error: parse error, expecting `'&'' or `"variable (T_VARIABLE)"' in /Users/russellharrower/Sites/ABCcars/plugins/function.selectdate.php on line 2
我想知道我该如何解决这个问题,因为我不需要发送任何$params,每次我去删除代码的那一部分时,它都会填充更多。
使用Smarty_Internal_Template$Template,而不是Smarty_Template_Instance
返回HTML,而不是回显HTML。
$returnHTML = '';
// Create HTML for years
foreach($years as $year)
{
$returnHTML .= '<option value="'.$year.'">'.$year . '</option>';
}
return $returnHTML;
您可能希望在运算符中创建整个select语句,以使其更易于使用。