Sådan benyttes komponenten Label klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Label.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Label::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Label($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Label klassen
Den fulde PHP kildekode for Label klassen
<?php/** * @package form * @see HTML_FORM_COMPONENT_PATH.'/Label.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.'/Span.php');require_once(HTML_BASE_UTIL_PATH.'/Element.php');require_once(HTML_BASE_UTIL_PATH.'/Accesskey.php');require_once(HTML_BASE_UTIL_PATH.'/Htmlspecialchars.php');/** * Generates a html form LABEL element * Note: disabled and readonly fields will also get an accelerator key * <code> * Usage: * $label = new Label($text, $for, $accesskey, $class); * print $label->getHtml(); * Or * Label::display($text, $for, $accesskey, $class); * </code> * @package form */class Label extends Html { /** * @var String $text The text to show */ protected $text = ''; /** * @var String $for The html element ID, for the asociated label */ protected $for = ''; /** * @var String $accesskey The accesskey class */ protected $accesskey = ''; /** * @var String $title The title */ protected $title = ''; /** * Constructor * @param String $text The text to show * @param String $for The ID for the control to relate to or false * @param String $accesskey The accesskey or false * @param String $class The class name */ function __construct($text, $for='', $accesskey='', $class='') { parent::__construct(); $this->text = ucfirst($text); $this->accesskey = ucfirst($accesskey != '' ? $accesskey : ($accesskey !== false ? Accesskey::next($this->text) : '')); if ($this->accesskey != '' && $accesskey !== false) { $this->title = 'Accelerator key, use (Alt + '.$this->accesskey.')'; } $this->class = $class != '' ? $class : ''; $this->for = $for != '' ? $for : ''; if ($this->for == '' && $for !== false) { if (defined('SHOW_ELEMENT_ID') && SHOW_ELEMENT_ID) { $this->for = Element::id($this->getClassName()); } } } /** * Returns the html for the label control * @return String the complete html */ function getHtml () { $html = $this->html; if ($this->text != '') { $html .= '<label'; $html .= $this->getAttribute('for'); $html .= $this->getAttribute('accesskey'); $html .= $this->getAttribute('class'); $html .= $this->getAttribute('title'); $html .= ">\r\n\t"; // In order to avoid line breaks and Translate html special chars $text = str_replace('_', ' ', $this->text); $text = Htmlspecialchars::encode(ucfirst($text)); $alt = ''; if ($this->accesskey != '' && $this->text != '') { /** * @todo strtoupper(...) Should be removed and strpos should be replaced by stripos * however this is only PHP 5.x compatible */ $pos = strpos(strtoupper($text),strtoupper($this->accesskey)); if ($pos !== false) { $before = substr($text, 0, $pos); $before = str_replace(' ',' ', $before); $after = substr($text, $pos + 1); $after = str_replace(' ',' ', $after); $access = substr($text, $pos, 1); $span = new Span($access, CSS_COLOR_DARK); $text = $before.$span->getHtml().$after; } $alt = ' (Alt + '.$this->accesskey.')'; } $html .= "<b>$text</b> $alt"; $html .= ' '.$this->getElements(); $html .= "</label><br />\r\n"; } else { if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "<!-- ".$this->getClassName()."->getHtml() Text for the label is empty -->\r\n"; } } return $html; } /** * Display html * <code> * Usage: * Label::display($text, $for, $accesskey, $class); * </code> * @static * @param String $text The text to show * @param String $for The ID for the control to relate to * @param String $accesskey The accesskey * @param String $class The class name */ public static function display($text, $for='', $accesskey='', $class='') { $html = new Label($text, $for, $accesskey, $class); $html->addHtml(); }}?>
Den fulde HTML kildekode for Label klassen
<? <!-- DEBUG: Label --> <label for="Label1" accesskey="T" title="Accelerator key, use (Alt + T)"> <b><span class="baseColorDark">T</span>est</b> (Alt + T) </label><br /> ?>
Her er 'klasse metoderne' for Label klassen:
Her er 'objekt variable' for Label klassen: