2012-07-31 64 views
2

我已經安裝了爲haml-and-sass和Yii中除了一個細節,一切都很正常:輸出不idented。PHamlP不縮進輸出

例如(注意,縮進tab

!!! 
%html(xmlns="http://www.w3.org/1999/xhtml",xml:lang="en",lang="en") 
    %head 
     %title="title" 
    /head 
    %body 
     #main 
      #banner 
       banner 
      /banner 
      #menu 
       menu 
      /menu 
      #content 
       content 
      /content 
      #footer 
       footer 
      /footer 
    /body 
/html 

輸出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 
title 
</title></head><!--head --> 
<body> 
<div id="main"> 
<div id="banner"> 
banner 
</div><!--banner --> 
<div id="menu"> 
menu 
</div><!--menu --> 
<div id="content"> 
content 
</div><!--content --> 
<div id="footer"> 
footer 
</div><!--footer --> 
</div></body><!--body --> 
</html><!--html --> 

PHamlP的配置指出ugly=false

'viewRenderer'=>array(
'class'=>'ext.phamlp.Haml', 
// delete options below in production 
'ugly' => false, 
'style' => 'nested', 
'debug' => 0, 
'cache' => false, 
), 

我見過other users有這個問題太但沒有解決方案回覆。

+0

我不個人建議這個擴展,SASS部分工作得很好。仍然更好地使用'sass watch',這個問題真的與HAML部分一樣,它使用了一些正則表達式,它們不能匹配所有真實的情況,它是一個不完整的端口。如果你想有一個PHP模板引擎,我可以推薦樹枝:http://twig.sensiolabs.org/只是MHO – Asgaroth 2012-10-03 13:47:01

+0

本身可能不實際工作的模塊,所以除非你解決的核心代碼,沒有什麼變量發生變化 – random 2012-11-11 06:11:46

回答

3

在HamlNestedRenderer.php你需要這個

private function getIndent($node) { 
    return str_repeat(' ', 2 * $node->line['indentLevel']); 
} 

改變這種

private function getIndent($node) { 
    return str_repeat(' ', 2 * $node->level); 
} 

之後,我改變了代碼一點的文件,因爲我仍然覺得難看。所以這裏是我的HamlNestedRenderer。評論線用於測試,您可以刪除它們。

class HamlNestedRenderer extends HamlRenderer { 
    /** 
    * Renders the opening tag of an element 
    */ 
    public function renderOpeningTag($node) { 
    // set default 
    $rot = '<!--default -->'; 
    // check whitespaceControl outer 
    if($node->whitespaceControl['outer'] == true){ 

     if (stripos(parent::renderOpeningTag($node), "html") !== false) { 
     $rot = ''; 
     }else{ 
     $rot = "\n"; 
     }   
    }else{ 
     $rot = $this->getIndent($node); 
    }; 
    // add indent 
    $rot .= $this->getIndent($node) ; 
    $rot .= parent::renderOpeningTag($node) ; 
    // check whitespaceControl inner 
    if($node->whitespaceControl['inner'] == true){ 
     // $rot .= '<!-- ROT 1.3 -->'; 
    }else{ 
     // check if isSelfClosing 
     if($node->isSelfClosing == true && $node->whitespaceControl['outer'] == true){ 
     // $rot .= '<!-- ROT 1.4 -->'; 
     }else{ 
     // $rot .= '<!-- ROT 1.5 -->'; 
     } 
    }; 
    // return var 
    return $rot; 
    } 

    /** 
    * Renders the closing tag of an element 
    */ 
    public function renderClosingTag($node) { 
    $rct = '<!-- default rct -->'; 
    if($node->isSelfClosing){ 
     $rct = ''; 
    }else{ 
     if($node->whitespaceControl['inner']){ 
     $rct = ''; 
     }else{ 
     // $rct = '<!-- RCT 1.3 -->'; 
     $rct = parent::renderClosingTag($node) ; 

     if($node->whitespaceControl['outer']){ 
      $rct .= "\n" . substr($this->getIndent($node), 0, -2); 
     }else{ 
      $rct .= "\n" ; 
     } 
     } 
    }; 
    return $rct;  
    } 

    /** 
    * Renders content. 
    * @param HamlNode the node being rendered 
    * @return string the rendered content 
    */ 
    public function renderContent($node) { 
    return parent::renderContent($node); 
    } 

    /** 
    * Renders the opening of a comment 
    */ 
    public function renderOpenComment($node) { 
    return parent::renderOpenComment($node) . (empty($node->content) ? "\n" : ''); 
    } 

    /** 
    * Renders the closing of a comment 
    */ 
    public function renderCloseComment($node) { 
    return parent::renderCloseComment($node) . "\n"; 
    } 

    /** 
    * Renders the start of a code block 
    */ 
    public function renderStartCodeBlock($node) { 
    return $this->getIndent($node) . parent::renderStartCodeBlock($node) . "\n"; 
    } 

    /** 
    * Renders the end of a code block 
    */ 
    public function renderEndCodeBlock($node) { 
    return $this->getIndent($node) . parent::renderEndCodeBlock($node) . "\n"; 
    } 

    private function getIndent($node) { 
    return str_repeat(' ', 2 * $node->level); 
    } 
} 
+0

謝謝,完美的作品! :) – 2013-01-09 07:27:18