2

我已经为 Yii 安装了haml-and-sass,除了一个细节之外,一切都很好:输出没有识别。

例如(注意缩进是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,
),

我已经看到其他用户也有这个问题,但在任何地方都没有解决方案。

4

1 回答 1

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);
  }
}
于 2012-12-23T15:50:05.233 回答