2013-08-21 18 views
0

我使用的是從https://github.com/XaminProject/handlebars.php把手PHP實現和嵌套的if/else發出

車把PHP實現在車把模板我使用嵌套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小白,我只是用這個Handelbars PHP實現有跨幾個VARIOS環境相同的模板。

你能幫我解決這個問題嗎?

感謝

+2

該庫與PHP 5.2不兼容,就這麼簡單。它與嵌套的任何東西無關。它使用匿名函數,因此至少需要PHP 5.3。 – deceze

+1

爲什麼近距離投票?是一個** SO **有效的問題,可以用於其他用戶! – diosney

+0

@deceze:我知道會很痛苦,但你能幫我把這些功能轉換成** 5.2 **有效嗎?我會嘗試在此之前聲明函數,並在此期間傳遞它,而不是匿名函數,但正如我所說的,我的PHP知識很少。 – diosney

回答

0

隨着@deceze和車把PHP項目的貢獻者之一(@everplays)的幫助下,我能找到一個解決的辦法:d

看到更多細節在https://github.com/XaminProject/handlebars.php/issues/16#issuecomment-23017993,希望我的推送請求將被接受並支持PHP 5.2至少在助手部分將被添加到項目中。

這是@everplays答案:

Just make helpers ( https://github.com/XaminProject/handlebars.php/blob/8eb732f407121392015b1bcf032f8e4287fb3969/src/Handlebars/Helpers.php#L67 ) static and register 'em like: $this->add('if, array('Handlebars_Helpers', '_if')) .

For example if helper (defined in line 71), should be like:

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

希望它會很快添加到主分支,因此它固定在主項目中。

感謝所有

+0

在'public static function _f'函數中:不是'_f' else'_if'。 –

+0

是的,現在編輯;)謝謝。 – diosney