如何在MustachePHP中使用自定义函数
我已经在我的项目中设置了如何在MustachePHP中使用自定义函数,php,mustache,mustache.php,Php,Mustache,Mustache.php,我已经在我的项目中设置了mustachephp echo $template->render(array( 'data'=>$data, 'lang'=>$lang, 'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) { return Common::getTextInHindi(ucwords(strtolower($helper-&g
mustachephp
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
包含字符串“已婚”
为什么会发生这种情况结果是必须对值进行修剪:
$GLOBALS['lang'][trim($maritialStatus)]
充其量,修剪之前已经完成,因此它已经以正确的格式存在:
echo $template->render(array(
'data'=>$data,
'lang'=>$lang,
'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) {
return trim(Common::getTextInHindi(ucwords(strtolower($helper->render($text)))));
}
));
是否可能情况不正确,因此变量
$martialStatus
的值为已婚
?在这种情况下,您可能需要编写$GLOBALS['lang'][ucfirst('maritialStatus)]
或$GLOBALS['lang'][ucfirst(strtolower('maritialStatus))]
另一个选项是必须修剪值:$GLOBALS['lang'][trim('maritialStatus])
trim完成了任务,谢谢……愚蠢的错误还有一件事你可以帮我……我将$lang传递给我的胡须模板,这也是我的$GLOBALS['lang']数组……因此,我可以在这里使用$lang来代替调用函数。当你改变函数中的值时(ucwords(strtolower($helper->render($text))
)我不知道如何调试,但如果调试$Globals['lang']
,所需的值已经以正确的格式存在。