««« Se kilde koden
triangle.gif Basic Base  Component Db Dto Form Form-elements Jquery Layout Menu Menu-fisheye Mvc Tab Table Template Util
 
arrow-headline.gif Index
 
 Tilbage

Navn : Tag.php


Sample code, tutorial

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();
    ?>

Parent html

Sådan vises komponenten Tag klassen

Hello world


PHP source code

Den fulde PHP kildekode for Tag klassen

<?
/**
* @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.10
* @since 22-feb-2007
*/

/**
* 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
$text    = ''; // The text to show
    
var $element = ''; // The html element
    
var $class   = ''; // The class name
    
var $name    = ''; // The name/id for a link identifier

    /**
     * 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 Tag($text='',$element='',$class='',$name='') {
        
$this->Html();
        if (
$text!= '') {
            if (
is_object($text)) {
                
$this->add($text);
            } else {
                
$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
'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(__FILE__,__LINE__,$msg);
                    } 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
'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(__FILE__,__LINE__,$msg);
                
$html .= TEXT_NO_DATA.'<!-- '.$this->getMsg(LOG_LEVEL_ERROR, $msg)." -->\r\n";
            } else {
                
$html .= TEXT_NO_DATA.'<!-- '.$msg." -->\r\n";
            }
        } else {
            
$html .= $this->getStart();
            
$html .= $this->getEnd();
        }
        return
$html;
    }

    
/**
     * Display start
     * <code>
     * Usage:
     *    Tag::start($element,$class);
     * </code>
     * @static
     * @param String $element The html element, if any
     * @param String $class   The css class name
     */
    
function start($element='',$class='') {
        
$html = new Tag('',$element,$class);
        
$html->addHtml($html->getStart());
    }

    
/**
     * Display end
     * <code>
     * Usage:
     *    Tag::end($element);
     * </code>
     * @static
     * @param String $element The html element, if any
     */
    
function end($element='') {
        
$html = new Tag('',$element);
        
$html->addHtml($html->getEnd());
    }

    
/**
     * Display html
     * <code>
     * Usage:
     *    Tag::display($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
     */
    
function display($text='',$element='',$class='',$name='') {
        
$html = new Tag($text,$element,$class,$name);
        
$html->addHtml();
    }
}
?>

HTML source code

Den fulde HTML kildekode for Tag klassen

<?
<h1>Hello world
</h1>

?>

Class methods

Her er 'klasse metoderne' for Tag klassen:

  • object
  • getclassname
  • getmsg
  • addhtml
  • gethtml
  • tostring
  • getcachefilename
  • save
  • content
  • html
  • setobject
  • set
  • get
  • getattribute
  • gettag
  • add
  • getsizeof
  • getelement
  • getelements
  • gettoogle
  • getmaximize
  • getminimize
  • newtriangle
  • display
  • showsource
  • tag
  • isvalid
  • getstart
  • getcontent
  • getend
  • start
  • end

Object vars

Her er 'objekt variable' for Tag klassen:

  • html =>
  • sql =>
  • elements => Array
  • sizeof => 1
  • text =>
  • element => h1
  • class =>
  • name =>

  triangle.gif

danmark

Germany

England

France

Italy

Norge

Sverige

USA