PHamlP no sangra la salida

He instalado haml-and-sass for Yii and everything works great except one detail: the output is not idented.

For instance (note that indentation is 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

Salidas

<!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's configuration states that ugly=false

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

He visto eso otros usuarios have this problem too but no solutions anywhere.

preguntado el 31 de julio de 12 a las 13:07

I don't personaly recomend this extension, the SASS part works just well. still its better to just use 'sass watch', the problem really its with the HAML part, it uses some regular expressions that wont match all the real cases, its an incomplete port. if you want a php template engine, I can recommend twig: ramita.sensiolabs.org just MHO -

The module itself may not actually work, so unless you fix the core code, there is nothing to change in variables -

1 Respuestas

In the file HamlNestedRenderer.php you need to change this

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

a este

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

After that I changed the code a bit, because I still found it ugly. So here is my HamlNestedRenderer. The commented lines were for testing, you could delete them.

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

Respondido el 23 de diciembre de 12 a las 15:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.