[英]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)))));
}
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.