我一直在尝试在对象上定义一个方法以用作 Mustache 模板的值,但 Mustache 模板没有正确调用它。所以我一定做错了什么。
这是一个例子:
<?php
require './vendor/mustache/mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
$t = new TplValues();
$t->planet = 'Earth';
$m = new Mustache_Engine();
echo $m->render('Hello, {{# caps}}{{planet}}{{/ caps}}!', $t);
class TplValues {
public function caps($text) {
return strtoupper($text);
}
}
这个的输出是:
PHP Warning: Missing argument 1 for TplValues::caps(), called in /home/user/test/vendor/mustache/mustache/src/Mustache/Context.php on line 138 and defined in /home/user/test/test.php on line 14
PHP Notice: Undefined variable: text in /home/user/test/test.php on line 15
Hello, !
我还尝试在构造函数中使用帮助器:
<?php
require './vendor/mustache/mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
$t = new stdClass();
$t->planet = 'Earth';
$m = new Mustache_Engine(array(
'helpers' => array(
'caps' => function($text) {return strtoupper($text);}
)
));
echo $m->render('Hello, {{# caps}}{{planet}}{{/ caps}}! ({{planet}})', $t);
这不会触发通知,但输出是:
Hello, !
我错过了什么吗?