Handlebars PHP实现和嵌套if / else问题

时间:2013-08-21 13:34:10

标签: php if-statement handlebars.js

我正在使用https://github.com/XaminProject/handlebars.php

中的Handlebars PHP 实施

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

 <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;
        }
    );

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

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

由于

2 个答案:

答案 0 :(得分:0)

使用@deceze和其中一个Handlebars PHP项目贡献者(@everplays)帮助我找到解决方案:D

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

这是@everplays的答案:

  

只做助手(   https://github.com/XaminProject/handlebars.php/blob/8eb732f407121392015b1bcf032f8e4287fb3969/src/Handlebars/Helpers.php#L67   )static并注册'em:$this->add('if, array('Handlebars_Helpers', '_if'))

     

例如,如果助手(在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

希望它很快就会被添加到主分支中,因此它已在主项目中修复。

感谢所有

答案 1 :(得分:0)

该库专为PHP 5.3及更高版本而设计。但是,如果你愿意的话,当我移植到我的平台时,我将这些部分重写为PHP 5.0及更高版本的所有工作:

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

享受!

相关问题