Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 06-02-2008 19:22:01

sekaijin
Membre
Date d'inscription: 17-08-2007
Messages: 1137

Génération de HTML

Salut à tous j'avais sous une pile de chose dans un coin de table perdu dans mon disque dur, couvert de toile d'araignée une classe pour générer du html. Je l’avais vaguement en php4 mais aussi une version JavaScript et une autre version jquery
Le principe est de générer le html à partir d’un objet par enchainement.

Code:

 <?php

ini_set('display_errors', true);
   error_reporting(E_ALL);

require_once dirname(__FILE__).'/Html.php';
$htmlDocument = new Document_Html('XHTML','Transitional');

$htmlDocument
   ->addNameSpace('fast', 'urn:org.jquery.fast')
   ->setNSAtrribute('fast','includepath', '/public/scripts/org.jquery.fast/components/')
   ->setLanguage('fr')
   ->getHead()
      ->contentType()
      ->meta ('test', 'machin')
      ->title('coucou', array('class'=>'test'))
      ->addScript('/public/scripts/org.jquery.fast/jquery-latest.js')
      ->addScript('/public/scripts/org.jquery.fast/org.jquery.fast.js')
      ->addScript('/public/scripts/org.jquery.fast/components/cascadingselect.js')
      ->linkStyle('/public/styles/default.css', 'screen')
      ->getDocument()
   ->getBody()
      ->aName('la','top')
         ->image('/truc/chose.gif', array('border'=>0), array('color'=>'#fff'))
            ->getParent()
         ->getParent()
      ->h2('coucou')
         ->getParent()
      ->inlineScript('<!--//')
         ->addContent("//alert('test');")
         ->addContent('//-->')
         ->getParent()
      ->aHref('ici','/truc/machin.html')
         ->getParent()
      ->br()
      ->form('/text.pg', 'POST')
         ->inputText('toto','5')
         ->label('test','25')->getParent()  
      ->pre('      ceci est un texte
      preformaté
      avec ce que ça implique')->br()
      ->addContent('test')->getParent()
      ->object()->getParent()
      ->table()
         ->thead()
            ->tr()
               ->th('coucou')->getParent()
               ->th('coucou')->getParent()
               ->th('coucou')->getParent()
               ->th('coucou')->getParent()
               ->getParent()
            ->getParent()
         ->tbody()
            ->tr()
               ->td()
                  ->b('test')->getParent()
                  ->getParent()
               ->td()->text('<test>')
;
$htmlDocument->display();
echo '<pre>';
print_r(htmlentities($htmlDocument->getHtml()));

Vu que certain parmi vous cherchent aussi à faire ce genre de chose j’ai décidé de la reprendre et la passer sous php5

Le tout pèse 35 Ko

Vous retrouverez dedans des méthodes reprenant le nom des tag html
Elles ont un prototype toujours semblable.
->tag( $attributes, $style) avec quelques variante par exemple les tag contenant habituellement un texte comme em
->em($text, $attributes, $style) et enfin quelque méthodes avec des non légèrement différent du tag variable (var étant un mot réservé)
Mais aussi sur les inputs inputText inputPassword etc… elle n’est pas documenté mais je pense déjà compréhensible.
Chaque appel retourne l’objet créé. Pour retrouver son père il suffit d’utiliser getParent() sauf pour quelques tag qui ne peuvent contenir d’élément comme <br> dans ce cas c’est le père qui est retourné.

Ceci permet l’enchainement comme dans l’exemple ci-dessus.

Si vous regardez dans le code vous trouverez dans le corps de ses méthodes trois booléen dans la construction du tag il servent à contrôler l’indentation du tag. Je na garantis pas dans l’état actuel une indentation parfaite.
J’ai traité à part le document (tag html) car il a de grosses spécificités

Code:

 <?php
require_once dirname(__FILE__).'/Html/Element.php';


class Document_Html {
   protected $_doctype;
   protected $_head;
   protected $_body;
   protected $_attributes = array ();
   protected $_nameSpaces = array();

   protected $_ElementClassName = 'Document_Html_Element';

   /**
   * Constructor
   * 
   * @param string $doctype Document Type (HTML|XHTML)
   * @param string $dtd Document Type Definition (Transitional...)
   * @param string $version Document Type Version (1.0...)
   * @return void
   */
   public function __construct($doctype = 'XHTML', $dtd = 'Transitional', $version ='1.0')
   {
      $this->setDocType($doctype, $dtd, $version);
      $this->_head = new $this->_ElementClassName($this, 'head', null, null, false, true, true);
      $this->_body = new $this->_ElementClassName($this, 'body', null, null, false, true, true);
   }

   public function setDocType($doctype = 'XHTML', $dtd = 'Transitional', $version ='1.0') {
      //TODO définir les règles pour générer les différente version du doctype
      /*
         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/transitional.dtd">
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//FR" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      */

      $this->_doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//FR" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; 
      $this->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
   }

   public function addNameSpace($prefix, $urn) {
      $this->setAttribute('xmlns:'.$prefix, $urn);
      $this->_nameSpaces[$prefix] = $urn;
      return $this;
   }

   public function setLanguage($lang) {
      $this->setAttribute('lang', $lang);
      $this->setAttribute('xml:lang', $lang);
      return $this;
   }

   public function setNSAtrribute($prefix, $name, $value) {
      if (!array_key_exists($prefix, $this->_nameSpaces)) {
         $this->addNameSpace($prefix, 'Undefined_Urn_'.rand());
      }
      $this->setAttribute($prefix.':'.$name, $value);
      return $this;
   }

   public function setAttribute($name, $value) {
      $this->_attributes[$name] = $value;
      return $this;
   }

   public function getHead() {
      return $this->_head;
   }

   public function getBody() {
      return $this->_body;
   }

   public function getHtml() {
      $src = $this->_doctype."\n";
      $src.= '<html '."\n";
      foreach ($this->_attributes as $name=>$value) {
         $src.= '   '.$name."='".$value."'\n";
      }
      $src.= ">";
      $src.= $this->getHead()->getHtml();
      $src.= $this->getBody()->getHtml();
      $src.= "\n</html>";
      return $src;
   }

   public function display() {
      echo $this->getHtml();
   }
}

Et enfin la génération de code

Code:

 <?php
//TODO ajouter la gestion de fragment

class Document_Html_Element {
   protected $_parent;
   protected $_tag;
   protected $_attributes = array ();
   protected $_styles = array();
   protected $_content = array();

   //indent system
   protected $_tab = '   ';
   protected $_indent = true;
   protected $_singleLine = false;
   protected $_indentContent = true;

   protected $_autoClose = true; //accepte <tag />

   /**
   * Constructor
   * 
   * @param Document_Html_Element $parent
   * @param string $tagName
   * @param string $attributes
   * @param string $styles
   * @return void
   */
   public function __construct($parent, 
                               $tagName = null, 
                               $attributes = null, 
                               $styles = null,
                               $singleLine = null,
                               $indent = null,
                               $identContent = null)
   {
      $this->_parent = $parent;
      if ($tagName)               $this->_tag = $tagName;
      if (is_array($attributes))  array_merge($this->_attributes = $attributes);
      if (is_array($styles))      array_merge($this->_styles = $styles);
      if (null !== $singleLine)   $this->_singleLine = $singleLine;
      if (null !== $identContent) $this->_indentContent = $identContent;
      if (null !== $indent)       $this->_indent = $indent;
   }

   public function getParent() {
      return $this->_parent;
   }

   public function getDocument() {
      require_once dirname(dirname(__FILE__)).'/Html.php';
      if ($this->_parent instanceOF Document_Html) {
         return $this->_parent;
      } else {
         return $this->_parent-> getDocument();
      }
   }

   public function forceClose() {
      $this->_autoClose = false;
      return $this;
   }

   public function setAttribute($name, $value) {
      $this->_attributes[$name] = $value;
      return $this;
   }

   Public function addContent($content) {
      $this->_content[] = $content;
      return $this;
   }

   function getHtml($tab = null) {
      $style = '';
      foreach ($this->_styles as $key=>$value) {
         $style.= $key.': '.$value.';';
      }

      if (null === $tab) $tab = $this->_tab;
      if ($this->_indent) {
         $src = "\n".$tab;
      } else {
         $src = '';
      }
      $src.= '<'.$this->_tag;
      foreach ($this->_attributes as $name=>$value) {
         $src.= ' '.$name."='".$value."'";
      }
      if ('' != $style) {
         $src.= " style='".$style."'";
      }
      if ($this->_autoClose&&(is_array($this->_content)&&(0 == count($this->_content)))) {
         $src.= ' />';
      } else {
         $src.= '>';
      }
      
      if (!(is_array($this->_content)&&(0 == count($this->_content)))||!$this->_autoClose){
         if ((is_array($this->_content))||
             (is_string($this->_content))) $src.= $this->_getHtmlContent($tab.$this->_tab);
   
         if ($this->_indent&&!$this->_singleLine) {
            $src.= "\n".$tab;
         }
         $src.= '</'.$this->_tag.'>';
      }
      return $src;
   }

   function _getHtmlContent($tab = null) {
      if (null === $tab) $tab = $this->_tab;
      if(is_array($this->_content)) {
         $src = '';
         foreach ($this->_content as $content) {
            if ($content instanceOf Document_Html_Element) {
               $src.= $content->getHtml($tab);
            } elseif (is_string($content)) {
               if ($this->_indentContent) {
                  $src.= "\n".$tab.$content;
               } else {
                  $src.= $content;
               }
            }
         }
         return $src;
      }
   }

   //Methodes pour créer des éléments.

   Public function Text($text = null) {
      $this->addContent(htmlentities($text));
      return $this;
   }

   Public function Space() {
      $this->addContent(' ');
      return $this;
   }

   Public function noBreakSpace($text = null) {
      $this->addContent('&nbsp;');
      return $this;
   }

   Public function aName($text = null, $name = null, $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $a = $this->inlineElement($this, 'a', $text, $attributes, $styles, true, true, false);
      $this->addContent($a);
      return $a;
   }

   Public function aHref($text = null, $href = null, $attributes = null, $styles = null) {
      $attributes['href'] = $href;
      $a = $this->inlineElement($this, 'a', $text, $attributes, $styles, true, true, false);
      $this->addContent($a);
      return $a;
   }

   Public function abbr($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'abbr', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function acronym($text = null, $title = null, $attributes = null, $styles = null) {
      $attributes['title'] = $title;
      $tag = $this->inlineElement($this, 'acronym', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function address($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'address', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function applet($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'applet', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function area($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'area', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function b($text= null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'strong', $text, $attributes, $styles, true, false, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function base($text= null, $href = null, $attributes = null, $styles = null) {
      $attributes['href'] = $href;
      $tag = $this->inlineElement($this, 'base', $text, $attributes, $styles, true, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function baseFont($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'basefont', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function bdo($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'bdo', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function big($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'big', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function blockquote($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'blockquote', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function br($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'br', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function nobr($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'nobr', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function button($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'button', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function caption($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'caption', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function center($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'center', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function cite($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'cite', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function code($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'code', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function col($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'col', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function colgroup($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'colgroup', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function dd($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'dd', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function del($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'del', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function dfn($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'dfn', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function dir($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'dir', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function div($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'div', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function dl($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'dl', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function dt($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'dt', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function em($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'em', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function fieldset($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'fieldset', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function font($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'font', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function form($action = null, $method = null, $attributes = null, $styles = null) {
      $attributes['action'] = $action;
      $attributes['method'] = $method;
      $tag = new Document_Html_Element($this, 'form', $attributes, $styles, false, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function frame($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'frame', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function frameset($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'frameset', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function h1($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h1', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function h2($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h2', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function h3($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h3', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function h4($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h4', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function h5($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h5', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function h6($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'h6', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function hr($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'hr', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function i($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'i', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function iframe($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'iframe', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function image($src = null, $attributes = null, $styles = null) {
      $attributes['src'] = $src;
      $img = new Document_Html_Element($this, 'img', $attributes, $styles, true, false, false);
      $this->addContent($img);
      return $img;
   }

   Public function input($type = null, $name = null, $value = null, $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $attributes['type'] = $type;
      $attributes['value'] = $value;
      $tag = new Document_Html_Element($this, 'input', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function inputText($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('text', $name, $value, $attributes, $styles);
   }

   Public function inputPassword($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('password', $name, $value, $attributes, $styles);
   }

   Public function inputCheckBox($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('checkbox', $name, $value, $attributes, $styles);
   }

   Public function inputRadio($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('radio', $name, $value, $attributes, $styles);
   }

   Public function inputSubmit($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('submit', $name, $value, $attributes, $styles);
   }

   Public function inputReset($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('reset', $name, $value, $attributes, $styles);
   }

   Public function inputFile($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('file', $name, $value, $attributes, $styles);
   }

   Public function inputHidden($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('hidden', $name, $value, $attributes, $styles);
   }

   Public function inputImage($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('image', $name, $value, $attributes, $styles);
   }

   Public function inputButton($name = null, $value = null, $attributes = null, $styles = null) {
      return $this->input('button', $name, $value, $attributes, $styles);
   }

   Public function ins($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'ins', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function isindex($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'isindex', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function kbd($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'kbd', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function label($text = null, $forId = null, $attributes = null, $styles = null) {
      $attributes['for'] = $forId;
      $tag = $this->inlineElement($this, 'label', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function legend($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'legend', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function li($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'li', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function link($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'link', $attributes, $styles, false, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function linkStyle($source = null, $media= null, $attributes = null, $styles = null) {
      $attributes['src'] = $source;
      $attributes['media'] = $media;
      $attributes['rel'] = 'stylesheet';
      $attributes['type'] = 'text/css';
      $tag = new Document_Html_Element($this, 'link', $attributes, $styles, false, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function map($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'map', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function menu($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'menu', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function meta($name='', $content='', $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $attributes['content'] = $content;
      $tag = new Document_Html_Element($this, 'meta', $attributes, $styles, true, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function contentType($content='text/html; charset=iso-8859-1', $attributes = null, $styles = null) {
      $attributes['http-equiv'] = 'Content-Type';
      $attributes['content'] = $content;
      $tag = new Document_Html_Element($this, 'meta', $attributes, $styles, true, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function noframes($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'noframes', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function noscript($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'noscript', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function object($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'object', $attributes, $styles, false, true, true);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function ol($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'ol', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function optgroup($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'optgroup', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function option($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'option', $attributes, $styles, false, true, true);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function p($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'p', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function param($name = null, $value = null, $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $attributes['value'] = $value;
      $tag = new Document_Html_Element($this, 'param', $attributes, $styles, false, true, false);
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function pre($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'pre', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function q($text = null, $cite = null, $attributes = null, $styles = null) {
      $attributes['cite'] = $cite;
      $tag = $this->inlineElement($this, 'q', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function s($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 's', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function samp($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'samp', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function inlineScript($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'script', $text, null, $attributes, $styles, true, true, true);
      $tag->forceClose();
      $this->addContent($tag);
      return $tag;
   }

   Public function script($source = null, $attributes = null, $styles = null) {
      $attributes['src'] = $source;
      $tag = new Document_Html_Element($this, 'script', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->addContent($tag);
      return $this;
   }

   /*
   Public function addInlineScript($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'script', $text, null, $attributes, $styles, true, true, true);
      $tag->forceClose();
      $this->getDocument()->getHead()->addContent($tag);
      return $this; //TODO rupture de fux 
      return $tag;
   }
   */

   Public function addScript($source = null, $attributes = null, $styles = null) {
      $attributes['src'] = $source;
      $tag = new Document_Html_Element($this, 'script', $attributes, $styles, true, true, false);
      $tag->forceClose();
      $this->getDocument()->getHead()->addContent($tag);
      return $this;
   }

   Public function select($name = null, $value = null, $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $attributes['value'] = $value;
      $tag = new Document_Html_Element($this, 'select', $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function small($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'small', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function span($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'span', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function strike($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'strike', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function strong($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'strong', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function addInlineStyle($text = null, $media= null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'style', $text, null, $media, $attributes, $styles, false, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function sub($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'sub', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function sup($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'sup', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function table($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'table', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function tbody($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'tbody', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function td($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'td', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function textarea($name = null, $text = null, $attributes = null, $styles = null) {
      $attributes['name'] = $name;
      $tag = $this->inlineElement($this, 'textarea', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function tfoot($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'tfoot', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function th($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'th', $text, $attributes, $styles, true, true, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function thead($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'thead', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function title($title = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'title', $title, $attributes, $styles,true, true, false);
      $this->addContent($tag);
      return $this;
   }

   Public function tr($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'tr', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function tt($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'tt', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function u($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'u', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }

   Public function ul($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'ul', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function varriable($text = null, $attributes = null, $styles = null) {
      $tag = $this->inlineElement($this, 'var', $text, $attributes, $styles, true, false, false);
      $this->addContent($tag);
      return $tag;
   }


   Public function body($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'body', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function head($attributes = null, $styles = null) {
      $tag = new Document_Html_Element($this, 'head', $attributes, $styles, false, true, true);
      $this->addContent($tag);
      return $tag;
   }

   Public function inlineElement($parent, 
                                 $tagName = null,
                                 $text = null, 
                                 $attributes = null, 
                                 $styles = null,
                                 $singleLine = null,
                                 $indent = null,
                                 $identContent = null)
   {
      $tag = new Document_Html_Element($parent, $tagName, $attributes, $styles, $singleLine, $indent, $identContent);
      $tag->addContent($text);
      return $tag;
   }
}

voilà si cela peut vous être utile.
le jour où j'aurais du temps je l'intègrerais à ZF en utilisant les Zend_Layout et Zend_Form

si vous l'intégré dans vos travaux, je sais que certaint d'entre vous on déjà des plugins Zend qui vont dans ce sens. penssez à nous dire comment vous faites évoluer la chose.

A+JYT

Dernière modification par sekaijin (08-02-2008 15:44:07)

Hors ligne

 

#2 06-02-2008 19:48:04

Mr.MoOx
Administrateur
Lieu: Toulouse
Date d'inscription: 27-03-2007
Messages: 1444
Site web

Re: Génération de HTML

Ouaip bien ça. Bien joué wink

Hors ligne

 

#3 12-02-2008 16:45:03

sekaijin
Membre
Date d'inscription: 17-08-2007
Messages: 1137

Re: Génération de HTML

je fais quelques expé sur cette approche et je vous poste un exemple de vue
main.php qui contient la page à générer et index.php qui contient le contenu pour le rendu du contrôleut par défaut

Code:

<?php
Zend_Loader::loadClass('Document_Html');

class View_Main extends Document_Html
{
   public function __construct()
   {
      #Fast_Debug::show('$this->data',$this->data);   
      //parent::__construct($doctype , $dtd, $version);
      parent::__construct();
   }

   public function make () {
      #Fast_Debug::show('$this->data',$this->data);   
      $this
         ->addNameSpace('fast', 'urn:org.jquery.fast')
         ->setNSAtrribute('fast','includepath', $this->data->baseUrl.'/public/scripts/org.jquery.fast/components/')
         ->setLanguage('fr')
         ->makeHead()
         ->getBody()
            ->div(array('id'=> 'page'))
               ->aName(' ','top')->getParent()
               ->makeBanner()
               ->makeMenu()
               ->makeMessage()
               ->makeContent()
               ->makeFooter()

      ;
   }

   public function makeHead () {
      $this->getHead()
            ->contentType()
            ->title($this->data->title)
            ->addScript($this->data->baseUrl.'/public/scripts/org.jquery.fast/jquery-latest.js')
            ->addScript($this->data->baseUrl.'/public/scripts/org.jquery.fast/org.jquery.fast.js')
            ->addScript($this->data->baseUrl.'/public/scripts/org.jquery.fast/components/cascadingselect.js')
            ->linkStyle($this->data->baseUrl.'/public/styles/default.css', 'screen')
      ;
      return $this;
   }

   public function makeBanner ($element) {
      $banner = $element->div(array('id'=> 'banner'));
      $banner->table(array('width'=>'100%', 
                   'border'=>0, 
                   'cellspacing'=>0, 
                   'cellpadding'=>0))
        ->tbody()
           ->tr()
              ->td(array('align'=>'left', 'height'=>52, 'valign'=>'top'))
                 ->image($this->data->baseUrl.'/public/images/orange_logo.gif', array('width'=>40, 'height'=>40))
                 ->getParent()
              ->td(array('class'=>'title', 'height'=>52, 'align'=>'center', 'valign'=>'top'))
                 ->span('Fast', array('class'=>'left'))
                    ->getParent()
                 ->text(' Application')
                    ->getParent()
              ->td(array('align'=>'right', 'height'=>52, 'valign'=>'top'))
                 ->image($this->data->baseUrl.'/public/images/esperluette.gif', array('width'=>32, 'height'=>35));
      
      $banner->table(array('width'=>'100%', 
                   'border'=>0, 
                   'cellspacing'=>0, 
                   'cellpadding'=>0))
        ->tbody()
           ->tr()
              ->td()
                 ->image($this->data->baseUrl.'/public/images/bureauxVitres.jpg', array('width'=>590, 'height'=>73))
                 ->getParent()
              ->td()
                 ->makeStats()
      ;
   }

   public function makeFooter ($element) {
      $footer = $element->div(array('id'=> 'piedDePage'))
         ->table(array('width'=>'100%', 
                      'border'=>0, 
                      'cellspacing'=>0, 
                      'cellpadding'=>0))
           ->tbody()
              ->tr();

      $footer->td(array('nowrap' => 'nowrap'))
         ->span(array('class' => 'diffusion'))
            ->text('interne Groupe France Télécom');

      $footer->td(array('class' => 'separateur'))->noBreakSpace();

      $bonhomme = $footer->td(array('nowrap' => 'nowrap'));
      if($this->data->user) {
         $bonhomme->homme();
      } else {
         $bonhomme->hommeGris();
      }

      $footer->td(array('class' => 'separateur'))->noBreakSpace();

      if($this->data->user) {
         $footer->td(array('nowrap' => 'nowrap'))
            ->flecheDroiteGris()
            ->text($this->data->user->usr_firstname . ' ' . $this->data->user->usr_name)
            ->br()
            ->aHref(null, $this->data->baseUrl.'/login/auth/newpwd')
               ->flecheDroite()
               ->text('Changer de mot de passe')
         ;
         $footer->td(array('class' => 'separateur'))->noBreakSpace();

         if ($this->data->user->profile) {
            $footer->td(array('nowrap' => 'nowrap'))
               ->flecheDroiteGris()
               ->text($this->data->user->profile->rol_label)
               ->br()
               ->flecheDroiteGris()
               ->text($this->data->user->profile->wkg_label);
         }
      }

      $connexion = $footer->td(array('nowrap' => 'nowrap'));
      if($this->data->user) {
         $connexion->flecheDroite()
               ->aHref('Déconnexion', $this->data->baseUrl.'/login/auth/logout')
               ->br()
               ->flecheDroite()
               ->aHref('Changer de profil', $this->data->baseUrl.'/login/auth/showRoles');
      } else {
         $connexion->flecheDroite()
               ->aHref('Connexion', $this->data->baseUrl.'/login/auth/showLogin');
      }

      $footer->td(array('class' => 'separateur'))->noBreakSpace();

      $footer->td(array('nowrap' => 'nowrap'))
         ->flecheDroite()
         ->aHref('Réalisé par ', 'http://metiertransverse.vente.francetelecom.fr/Site.do?position=FAST&site=Processus_France', array('target' => '_blank'))
            ->fastLogo();

      $footer->td(array('class' => 'separateur'))->noBreakSpace();

      $footer->td(array('nowrap' => 'nowrap'))
         ->flecheDoubleHaut()
         ->aHref('haut de page', '#top');

   }


   public function makeStats ($element) {
      $element->table(array( 'width'       => '90%',
                             'height'      => 73,
                             'align'       => 'right', 
                             'cellspacing' => 0, 
                             'cellpadding' => 3),
                      array('background-color' => '#EEEEEE', 
                            'border'           => 'solid 1px #DDDDDD',
                            'margin-bottom'    => '3px')
                     )
         ->tbody()
            ->tr()
               ->td('My App en chiffres', null, 
                  array('background-color'=>'#DDDDDD', 
                        'font-weight' => 'bold'))
                  ->getParent()
               ->getParent()
            ->tr()
               ->td('(x) remontées déposées')
                  ->getParent()
               ->getParent()
            ->tr()
               ->td('(x) remontées concrétisées')
                  ->getParent()
               ->getParent()
               
      ;
   }

   public function makeMenu ($element) {
      if (isset($this->data->menus)) {
         $nmuContent = $element
            ->table(array('class'       => 'menus', 
                          'border'      => 0, 
                          'cellspacing' => 0, 
                          'cellpadding' => 0))
               ->tbody()
                  ->tr();
         foreach ($this->data->menus as $menu) {
            $mnuItem = $nmuContent->td(array('title', $menu->help));
            if ('true' == $menu->actif) {
               $mnuItem->aHref(null,$this->data->baseUrl.'/'.$menu->url)
                  ->flecheDroite()
                  ->text($menu->label);
            } else {
               $mnuItem->aHref(null,'#', array('onClick' => 'return false;',
                                               'class' => 'inactif'
                                              ))
                  ->flecheDroite()
                  ->text($menu->label);
            }
         }
      }
   }

   public function makeMessage($element) {
      $msg = $element->div(array('id' => 'message'));
      if ($this->data->messages) {
         $msg->div(array('class' => 'hd'))
            ->div(array('class' => 'c'));

         $msgitem = $msg->div(array('class' => 'bd'))
            ->div(array('class' => 'c'))
               ->div(array('class' => 's'));

         foreach ($this->data->messages as $message) {
            $msgitem->li($message->label, array('class'=>'type'));
         }

         $msg->div(array('class' => 'ft'))
            ->div(array('class' => 'c'));
      }              
   }

   public function flecheDoubleHaut ($element) {
      $element->image($this->data->baseUrl.'/public/images/flecheDoubleHaut.gif', array('width'=>8, 'height'=>8));
   }

   public function flecheDroite ($element) {
      $element->image($this->data->baseUrl.'/public/images/flecheDroite.gif', array('width'=>8, 'height'=>8));
   }

   public function flecheDroiteGris ($element) {
      $element->image($this->data->baseUrl.'/public/images/flecheDroiteGris.gif', array('width'=>8, 'height'=>8));
   }

   public function flecheGauche ($element) {
      $element->image($this->data->baseUrl.'/public/images/flecheGauche.gif', array('width'=>8, 'height'=>8));
   }

   public function homme ($element) {
      $element->image($this->data->baseUrl.'/public/images/bonhomme.gif', array('width'=>18, 'height'=>24, 'align'=>'left'));
   }

   public function hommeGris ($element) {
      $element->image($this->data->baseUrl.'/public/images/bonhommeGris.gif', array('width'=>18, 'height'=>24, 'align'=>'left'));
   }

   public function fastLogo ($element) {
      $element->image($this->data->baseUrl.'/public/images/logo_fast_h10.gif', array('width'=>29, 'height'=>10, 'alt'=>'fast'));
   }

   public function makeContent($content) {
      include $this->data->_content;
   }

}

Code:

<?php
$content->div(array('id'=>'content'))
   ->text("Voici le contenu correspondant à l'action 'index' du controlleur 'index'.");

la première chose que je retire de ce test c'est que il vaut mieux faire beaucoup de fonction spécialisé que d'écrire tout d'un bloc c'est plus facile à gérer
mais le code doit être serieursement documenté pour donner une idée du résultat car ce n'est pas évident.

par contre le passage de variable est l'insertion de donnée est bien plus simple qu'en phtml
j'ai pris une approche macro par exemple makeMessage() qui se débrouille seul
une approche plus fonctionnelle devrait être plus claire displayMessages($this->data->messages)

A+JYT

Dernière modification par sekaijin (12-02-2008 16:45:25)

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages