5

I have set up mustache php in my project.

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

and my user defined function is

public static function getTextInHindi($maritialStatus) {
      return $GLOBALS['lang'][$maritialStatus];
}

Now in my user defined function as you can see above when I try to print

print_r($GLOBALS['lang']['Married']);  //gives correct output
print_r($GLOBALS['lang'][$maritialStatus]); //gives undefined index error

even though $maritialStatus contains the string 'Married'.

Why is this happening

Dean Ambrose
  • 183
  • 10
  • is it possible that the case is not right, so that the value of the variable `$martialStatus` is `married`? In that case perhaps you'd to write `$GLOBALS['lang'][ucfirst($maritialStatus)]` or `$GLOBALS['lang'][ucfirst(strtolower($maritialStatus))]` – David Jun 17 '18 at 06:47
  • Another option is that you have to trim the value: `$GLOBALS['lang'][trim($maritialStatus)]` – David Jun 17 '18 at 06:50
  • 1
    trim did the job thanks......silly mistakes – Dean Ambrose Jun 17 '18 at 06:54

1 Answers1

2

Turned out the value had to be trimmed:

 $GLOBALS['lang'][trim($maritialStatus)]

At best trimming is already done before, so that it exists in the right format already:

echo $template->render(array(
     'data'=>$data, 
     'lang'=>$lang,
     'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) {
       return trim(Common::getTextInHindi(ucwords(strtolower($helper->render($text)))));
      }
));
David
  • 5,882
  • 3
  • 33
  • 44
  • 1
    one more thing you can help me here....I am passing $lang to my mustache template which is also my $GLOBALS['lang'] array...so instead of calling the function is there any I can use $lang here. – Dean Ambrose Jun 17 '18 at 06:58
  • As you alter the value in your function (`ucwords(strtolower($helper->render($text))`) I don't see how, but perhaps if you debug `$Globals['lang']` the desired value exists already in the right format. – David Jun 17 '18 at 07:02