2017-02-16 104 views
0

我想給一個變量傳遞給一個函數:PHP鬍子傳遞變量的函數

{{#myFunc}}{{someVar}}{{/myFunction}} 

這是我的引擎:

$options = [ 
    'helpers' => [ 
     'myFunc' => function($value) { 
      // This works 
      return 'got value: ' . $value; 

      // This does not work 
      return SomeClass::reformat($value); 
      // It passes "{{someVar}}" to the method, not the value 

      // var_dump shows 
      var_dump($value); // '{{someVar}}' 
     } 
    ] 
]; 
$engine = new Mustache_Engine($options); 

$rendered = $engine->render('{{#myFunc}}{{someVar}}{{/myFunction}}', ['someVar' => '12345']); 

我該怎麼辦?

回答

-1

您需要使用lambda,here你可以看到一個例子:

//the data 
$data = array(
    'someVar' => "12345", 
    'myFunc' => function($var) { 
     return "got Value:" . $var; 
    } 
); 
$engine = new Mustache_Engine(); 
$rendered = $engine->render('{{#myFunc}}{{someVar}}{{/myFunc}}', $data); 

的功能是$data內。