You are here

class HamlRenderer in Sassy 7

HamlRenderer class. Provides the most common version of each method. Child classs override methods to provide style specific rendering. @package PHamlP @subpackage Haml.renderers

Hierarchy

Expanded class hierarchy of HamlRenderer

File

phamlp/haml/renderers/HamlRenderer.php, line 24

View source
class HamlRenderer {

  /**#@+
   * Output Styles
   */
  const STYLE_COMPRESSED = 'compressed';
  const STYLE_COMPACT = 'compact';
  const STYLE_EXPANDED = 'expanded';
  const STYLE_NESTED = 'nested';

  /**#@-*/
  const INDENT = '  ';
  private $format;
  private $attrWrapper;
  private $minimizedAttributes;

  /**
   * Returns the renderer for the required render style.
   * @param string render style
   * @return HamlRenderer
   */
  public static function getRenderer($style, $options) {
    switch ($style) {
      case self::STYLE_COMPACT:
        return new HamlCompactRenderer($options);
      case self::STYLE_COMPRESSED:
        return new HamlCompressedRenderer($options);
      case self::STYLE_EXPANDED:
        return new HamlExpandedRenderer($options);
      case self::STYLE_NESTED:
        return new HamlNestedRenderer($options);
    }

    // switch
  }
  public function __construct($options) {
    foreach ($options as $name => $value) {
      $this->{$name} = $value;
    }

    // foreach
  }

  /**
   * Renders element attributes
   */
  private function renderAttributes($attributes) {
    $output = '';
    foreach ($attributes as $name => $value) {
      if (is_integer($name)) {

        // attribute function
        $output .= " {$value}";
      }
      elseif ($name == $value && in_array($name, $this->minimizedAttributes) && ($this->format === 'html4' || $this->format === 'html5')) {
        $output .= " {$name}";
      }
      elseif (in_array($name, $this->minimizedAttributes)) {

        // $value is a variable, isset is called to make sure E_NOTICE is not thrown
        if (preg_match("", trim($value))) {
          $output .= "<" . "?php if(isset({$value}) && !empty({$value})): ?" . "> {$name}={$this->attrWrapper}<" . "?php echo {$value}; ?" . ">{$this->attrWrapper};<" . "?php endif; ?" . ">";
        }
        else {
          $output .= "<" . "?php if({$value}): ?" . "> {$name}={$this->attrWrapper}<" . "?php echo {$value}; ?" . ">{$this->attrWrapper};<" . "?php endif; ?" . ">";
        }
      }
      else {
        $output .= " {$name}={$this->attrWrapper}{$value}{$this->attrWrapper}";
      }
    }
    return $output;
  }

  /**
   * Renders the opening tag of an element
   */
  public function renderOpeningTag($node) {
    $output = "<{$node->content}";
    $output .= $this
      ->renderAttributes($node->attributes);
    $output .= ($node->isSelfClosing ? ' /' : '') . '>';
    return $output;
  }

  /**
   * Renders the closing tag of an element
   */
  public function renderClosingTag($node) {
    return $node->isSelfClosing ? '' : "</{$node->content}>";
  }

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

  /**
   * Renders the closing of a comment
   */
  public function renderCloseComment($node) {
    return ($node->isConditional ? "\n<![endif]" : ' ') . '-->' . ($node->isConditional ? "\n" : '');
  }

  /**
   * Renders the start of a code block
   */
  public function renderStartCodeBlock($node) {
    return $this
      ->renderContent($node);
  }

  /**
   * Renders the end of a code block
   */
  public function renderEndCodeBlock($node) {
    return '<?php }' . (!empty($node->doWhile) ? " {$node->doWhile}" : '') . ' ?>';
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
HamlRenderer::$attrWrapper private property
HamlRenderer::$format private property
HamlRenderer::$minimizedAttributes private property
HamlRenderer::getRenderer public static function * Returns the renderer for the required render style. *
HamlRenderer::INDENT constant
HamlRenderer::renderAttributes private function * Renders element attributes
HamlRenderer::renderCloseComment public function * Renders the closing of a comment 2
HamlRenderer::renderClosingTag public function * Renders the closing tag of an element 4
HamlRenderer::renderContent public function * Renders content. * 2
HamlRenderer::renderEndCodeBlock public function * Renders the end of a code block 2
HamlRenderer::renderOpenComment public function * Renders the opening of a comment 2
HamlRenderer::renderOpeningTag public function * Renders the opening tag of an element 4
HamlRenderer::renderStartCodeBlock public function * Renders the start of a code block 2
HamlRenderer::STYLE_COMPACT constant
HamlRenderer::STYLE_COMPRESSED constant
HamlRenderer::STYLE_EXPANDED constant
HamlRenderer::STYLE_NESTED constant
HamlRenderer::__construct public function