【问题标题】:PHamlP does not indent the outputPHamlP 不缩进输出
【发布时间】:2012-07-29 05:21:10
【问题描述】:

我已经为 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,
),

我看到other users也有这个问题,但没有任何解决方案。

【问题讨论】:

  • 我个人不推荐这个扩展,SASS 部分工作得很好。仍然最好只使用'sass watch',问题真的出在HAML部分,它使用了一些不匹配所有真实案例的正则表达式,它是一个不完整的端口。如果你想要一个 php 模板引擎,我可以推荐 Twig:twig.sensiolabs.org just MHO
  • 模块本身可能实际上并不能正常工作,所以除非你修复核心代码,否则变量没有任何变化

标签: php web-applications yii haml yii-extensions


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多