处理PHP实现和嵌套的if/else问题


Handlebars PHP implementation and nested if/else issues

我使用的是https://github.com/XaminProject/handlebars.php

在我使用嵌套if/else的Handlebars模板中,请参阅下面的模板:

 <div class="text-align-{{ options.alignment }} border-bottom-{{ options.style }}" style="border-width: {{ options.width }}px; border-color: {{ options.color }}">
    {{#if options.use_title_separator}}
        <div>
            {{#if options.back_to_top}}
                <a href="" onclick="return false;">{{ options.text_label }}</a>
            {{else}}
                {{ options.text_label }}
            {{/if}}
        </div>
    {{/if}}
</div>

它在PHP 5.4安装中运行良好,但在PHP 5.2的安装中会抛出以下错误:

<b>Parse error</b>: syntax error, unexpected T_FUNCTION in <b>/.../Handlebars/Helpers.php</b> on line <b>71</b><br />

冲突代码似乎是:

$this->add(
        'if', 
        function ($template, $context, $args, $source) {
            $tmp = $context->get($args);
            $buffer = '';
            if ($tmp) {
                $template->setStopToken('else');
                $buffer = $template->render($context);
                $template->setStopToken(false);
                $template->discard($context);
            } else {
                $template->setStopToken('else');
                $template->discard($context);
                $template->setStopToken(false);
                $buffer = $template->render($context);
            }
            return $buffer;
        }
    );

我是一个完整的PHPnoob,我只是使用这个Handelbars PHP实现在几个不同的环境中拥有相同的模板。

你能帮我解决这个问题吗?

感谢

有了@decze和Handlebars PHP项目贡献者之一(@everplays)的帮助,我可以找到一个解决方案:D

更多详细信息,请访问https://github.com/XaminProject/handlebars.php/issues/16#issuecomment-23017993,希望我的推送请求能被接受,并将至少在helpers部分对PHP 5.2的支持添加到项目中。

这是来自@everplays:的答案

只是做帮手(https://github.com/XaminProject/handlebars.php/blob/8eb732f407121392015b1bcf032f8e4287fb3969/src/Handlebars/Helpers.php#L67)CCD_ 2和寄存器'em类似:CCD_。

例如,如果helper(在line 71中定义)应该像:

public static function _if($template, $context, $args, $source) {
  // body is the same
}

因此,我对addDefaultHelpers()函数中的所有当前助手重复了这种模式,如下所示:

/**
 * Create handler for the 'with' helper.
 * Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
 *
 * @param $template
 * @param $context
 * @param $args
 * @param $source
 *
 * @return mixed
 */
  public static function _helper_with($template, $context, $args, $source) {
    $tmp = $context->get($args);
    $context->push($tmp);
    $buffer = $template->render($context);
    $context->pop();
    return $buffer;
}
   ...
protected function addDefaultHelpers()
{
    $this->add(
        'if',
        array('Handlebars_Helpers', '_helper_if')
    );
    $this->add(
        'each',
        array('Handlebars_Helpers', '_helper_each')
    );
    $this->add(
        'unless',
        array('Handlebars_Helpers', '_helper_unless')
    );
    $this->add(
        'with',
        array('Handlebars_Helpers', '_helper_with')
    );
    //Just for compatibility with ember
    $this->add(
        'bindAttr',
        array('Handlebars_Helpers', '_helper_bindAttr')
    );
}

并进入以下版本的Helpers文件:

https://github.com/XaminProject/handlebars.php/pull/17/commits

希望它能很快添加到主分支中,以便在主项目中固定。

感谢所有

该库是为PHP 5.3及更高版本设计的。然而,如果你愿意的话,当移植到我的平台时,我重写了这些部分,使它们都能使用PHP 5.0及更高版本

https://github.com/EGreg/Q/tree/master/platform/classes/Handlebarshttps://github.com/EGreg/Q/blob/master/platform/classes/Q/Handlebars.php

享受吧!