Sådan benyttes komponenten Tag klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Tag.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Tag::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Tag($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Tag klassen
Den fulde PHP kildekode for Tag klassen
<?php/** * @package base * @filesource * @see HTML_BASE_UTIL_PATH.'/Tag.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');require_once(HTML_BASE_UTIL_PATH.'/Raw.php');require_once(HTML_BASE_UTIL_PATH.'/Htmlspecialchars.php');if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * Generates a Tag object, surronded by the specified html tag element * <code> * Usage: * $text = 'Hello world'; * $element = 'h1'; * $tag = new Tag($text, $element, $class, $name); * print $tag->getHtml(); * Or * Tag::display($text, $element, $class, $name); * </code> * @package base */class Tag extends Html { /** * @var String $text The text to show */ protected $text = ''; /** * @var String $element The html element */ protected $element = ''; /** * @var String $class The CSS class name */ protected $class = ''; /** * @var String $name The name/id for a link identifier */ protected $name = ''; /** * Constructor * @param String $text The text to show * @param String $element The html element, if any * @param String $class The css class name * @param String $name The name/id attribute for a link identifier */ function __construct($text='', $element='', $class='', $name='') { parent::__construct(); if (is_object($text)) { $this->add($text); } else { if ($text !== '') { $this->add(new Raw($text)); // Plain text object } } $this->element = $element; $this->class = $class; $this->name = $name; $this->isValid(); } /** * Validates the Tag */ function isValid() { if ($this->element !== '') { switch ($this->element) { case 'legend': case 'pre': case 'div': case 'span': case 'p': case 'hr': case 'br': case 'ol': case 'ul': case 'li': case 'b': case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': // Ok break; default: $msg = $this->getClassName().' unsupported html element, found='.$this->element; if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); } else { } // TODO what ? die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".$this->getMsg(LOG_LEVEL_FATAL, $msg)); } } } /** * Returns the start html for the text, optional surronded by the html element * @return String the complete html */ function getStart() { $html = ''; if ($this->element != '') { if ($this->element === 'br' || $this->element === 'hr') { // <br />, <hr /> See getEnd() } else { $html .= $this->html; $html .= '<'.$this->element; $html .= $this->getAttribute('class'); $html .= ">"; switch ($this->element) { case 'legend': case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': case 'p' : case 'li': // Ignore \r\n break; default: $html .= "\r\n"; break; } } } $html .= $this->getContent(); return $html; } /** * Returns the content html for the text, optional surronded by the html element * @return String the complete html */ function getContent() { $html = ''; if ($this->text !== '') { if ($this->name !== '') { switch ($this->element) { case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': // i.e. <h1><a name=""name>text</a></h1> $link = new Link($this->text,'','','','','', $this->name); $html .= $link->getHtml(); break; default: // Ignore, Use standard $html .= Htmlspecialchars::encode($this->text); // Translate html special chars break; } } else { $html .= Htmlspecialchars::encode($this->text); // Translate html special chars } } $html .= $this->getElements(); return $html; } /** * Returns the end html for the text, optional surronded by the html element * @return String the complete html */ function getEnd() { $html = ''; if ($this->element !== '') { if ($this->element === 'br' || $this->element === 'hr') { $html .= '<'.$this->element." />\r\n"; // <br /> OR <hr /> } else { $html .= '</'.$this->element.">\r\n"; } } return $html; } /** * Returns the html for the text, optional surronded by the html element * @return String the complete html */ function getHtml() { $html = ''; if ($this->text == '' && $this->element == '' && $this->getSizeof() == 0) { $msg = $this->getClassName().'->getHtml(), no text or elements found'; if (defined('HTML_LOG_UTIL_PATH')) { Log::warn($msg, __FILE__, __LINE__); $html .= TEXT_NO_DATA.'<!-- '.$this->getMsg(LOG_LEVEL_ERROR, $msg)." -->\r\n"; } else { $html .= TEXT_NO_DATA.'<!-- '.$msg." -->\r\n"; } Message::add($msg, __FILE__, __LINE__); } else { $html .= $this->getStart(); $html .= $this->getEnd(); } return $html; } /** * BECAUSE of incompatible methods, this method MUST be re-written, so the $element is the very last * Display start * <code> * Usage: * Tag::starting($element, $class); * </code> * @static * @param String $element The html element, if any * @param String $class The css class name */ public static function starting($element='', $class='') { $html = new Tag('', $element, $class); $html->addHtml($html->getStart()); } /** * BECAUSE of incompatible methods, this method MUST be re-written, so the $element is the very last * Display end * <code> * Usage: * Tag::ending($element); * </code> * @static * @param String $element The html element, if any */ public static function ending($element='') { $html = new Tag('', $element); $html->addHtml($html->getEnd()); } /** * BECAUSE of incompatible methods, this method MUST be re-written, so the $element is the very last * Display html * <code> * Usage: * Tag::displaying($text, $element, $class, $name); * </code> * @static * @param String $text The text to show * @param String $element The html element, if any * @param String $class The css class name * @param String $name The name/id attribute for a link identifier */ public static function displaying($text='', $element='', $class='', $name='') { $html = new Tag($text, $element, $class, $name); $html->addHtml(); }}?>
Den fulde HTML kildekode for Tag klassen
<? <!-- DEBUG: Tag --> <h1>Hello world </h1> ?>
Her er 'klasse metoderne' for Tag klassen:
Her er 'objekt variable' for Tag klassen: