php - PHamlP 不缩进输出

标签 php web-applications yii haml yii-extensions

我已经安装了haml-and-sass对于 Yii 来说,一切都很好,除了一个细节:输出未识别。

例如(注意缩进是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也有这个问题,但没有解决办法。

最佳答案

在文件 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);
  }
}

关于php - PHamlP 不缩进输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11740169/

相关文章:

使用私有(private)类函数/变量的 PHP 静态方法

performance - 我应该使用 SmartGWT 还是 Vaadin?

java - 对Java文件,JSP,Servlet的集成感到困惑吗?

php - 如何在 Yii 2.0.6 中激活 Gii 代码生成器?

Yii 1.1 - 创建带有验证的多步骤表单

php - 如何使用 php 变量在 MySQL 查询的 WHERE 条件中使用变量?

php - 如何在 WooCommerce 中出现 "proceed to checkout"时清除通知

javascript - Ajax跨域php代理403错误

javascript - 如何防止用户从本地保存的 Html 登录页面访问 Web 应用程序?