【发布时间】:2018-11-26 09:38:03
【问题描述】:
我已经在我的项目中设置了mustache php。
echo $template->render(array(
'data'=>$data,
'lang'=>$lang,
'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) {
return Common::getTextInHindi(ucwords(strtolower($helper->render($text))));
}
));
我的用户定义函数是
public static function getTextInHindi($maritialStatus) {
return $GLOBALS['lang'][$maritialStatus];
}
现在在我的用户定义函数中,当我尝试打印时,您可以在上面看到
print_r($GLOBALS['lang']['Married']); //gives correct output
print_r($GLOBALS['lang'][$maritialStatus]); //gives undefined index error
即使$maritialStatus 包含字符串'Married'。
为什么会这样
【问题讨论】:
-
会不会是大小写不对,所以变量
$martialStatus的值为married?在这种情况下,您可能会写$GLOBALS['lang'][ucfirst($maritialStatus)]或$GLOBALS['lang'][ucfirst(strtolower($maritialStatus))] -
另一种选择是您必须修剪值:
$GLOBALS['lang'][trim($maritialStatus)] -
修剪完成了工作,谢谢......愚蠢的错误
标签: php mustache mustache.php