使用 php 为渲染车把创建帮助程序


Create helper for render handlebars using php

我有一个大问题,我不知道如何解决这个问题。所以我有一个助手类:

class IfCondHelper implements HelperInterface
{
public function execute(Template $template, HandlebarsContent $context, $args, $source)
{
    $parsed_args = $template->parseArguments($args);
    if (count($parsed_args) != 3) {
        throw new 'InvalidArgumentException(
            '"IfCond" helper expects exactly three arguments.'
        );
    }
    switch ($context->get($parsed_args[1])) {
        case "==":
            return ($context->get($parsed_args[0]) == $context->get($parsed_args[2])) ? $source : false;
            break;
..............
     }
}

现在在我的模板中,我做:

{{#ifCond 2 '==' 2}} {{data.oUser.number}} {{/ifCond}}

问题是模板没有显示 data.oUser.number whitch 的值为 4,而是显示代码data.oUser.number whitout 解释它们。助手工作正常,因为如果我这样做:

{{#ifCond 2 '==' 2}} <p>Test</p> {{/ifCond}} 

这工作正常。你能帮我吗?提前感谢,对不起我的英语

我发现了错误,调用助手后需要做一个补充渲染

return ($context->get($parsed_args[0]) == $context->get($parsed_args[2])) ? $template->render($context) : false;