如何在小胡子中使用用户定义的函数 php

How to use user defined function in mustache php

我已经在我的项目中设置了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'.

为什么会这样

原来必须修剪该值:

 $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)))));
      }
));