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

Dea*_*ose 5 php mustache 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))));
      }
));
Run Code Online (Sandbox Code Playgroud)

我的用户定义的功能是

public static function getTextInHindi($maritialStatus) {
      return $GLOBALS['lang'][$maritialStatus];
}
Run Code Online (Sandbox Code Playgroud)

现在在我的用户定义函数中,我在上面尝试打印时可以看到

print_r($GLOBALS['lang']['Married']);  //gives correct output
print_r($GLOBALS['lang'][$maritialStatus]); //gives undefined index error
Run Code Online (Sandbox Code Playgroud)

即使$maritialStatus包含字符串'Married'.

为什么会这样呢?

Dav*_*vid 2

事实证明该值必须被修剪:

 $GLOBALS['lang'][trim($maritialStatus)]
Run Code Online (Sandbox Code Playgroud)

最好的情况是之前已经完成了修剪,以便它已经以正确的格式存在:

echo $template->render(array(
     'data'=>$data, 
     'lang'=>$lang,
     'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) {
       return trim(Common::getTextInHindi(ucwords(strtolower($helper->render($text)))));
      }
));
Run Code Online (Sandbox Code Playgroud)