Sådan benyttes komponenten Meta klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Meta.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Meta::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Meta($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Meta klassen
Den fulde PHP kildekode for Meta klassen
<?php/** * @package base * @filesource * @see HTML_BASE_PAGE_PATH.'/Meta.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');/** * The Meta Object is used to define the html meta tags for a page * NOTE: The header() function is called inside the file * @see Cache.php if no cache is defined * <code> * Usage: * $meta = new Meta($title, $description, $keyword, $keywords, $language); * print $meta->getHtml(); * Or * print $meta->getHttpEquiv($name); * Or * print $meta->getContent($name); * Or * $meta = new Meta(); * $meta->setHttpEquiv($name, $value); * I.e. * $meta->setHttpEquiv("refresh","1;URL=http://finn-rasmussen.com/"); * print $meta->getHttpEquiv("refresh"); * Or * Meta::display($title, $description, $keyword, $keywords, $language); * </code> * @package base */class Meta extends Html { /** * @var String $keyword Internal usage, not part of the meta tags */ protected $keyword = ''; /** * @var String $title The Meta Title for this page */ protected $title = ''; protected $description = ''; protected $keywords = ''; protected $robots = ''; protected $copyright = ''; protected $author = ''; protected $Content_Type = ''; protected $Content_Script_Type = ''; protected $Content_Language = ''; protected $Expires = ''; protected $Cache_Control = ''; protected $Pragma = ''; protected $resource_type = ''; protected $distribution = ''; protected $revisit_after = ''; // IE specific protected $Site_Enter = ''; protected $Site_Exit = ''; protected $Page_Enter = ''; protected $Page_Exit = ''; /** * @var String $refresh The meta refresh url to redirect to content="time;url"" */ protected $refresh = ''; /** * @var String $content The meta content */ protected $content = ''; /** * Constructor * @param String $title The title of the page * @param String $description The description of the page * @param String $keyword The unique keyword for this page (internal usage) * @param String $keywords The keywords for the page * @param String $language The language for the page */ function __construct($title='', $description='', $keyword='', $keywords='', $language='') { parent::__construct(); $this->keyword = $keyword != '' ? $keyword : PAGE_KEYWORD; // Unique keyword for this page (internal usage) $this->title = $title != '' ? $title : PAGE_TITLE; if ($this->keyword != '' && strpos($this->title, $this->keyword) === false) { $this->title = $keyword.' - '.$this->title; } if (strpos($this->title, DOMAIN_NAME) === false) { $this->title .= ' (+) '.DOMAIN_NAME; } $this->description = $description != '' ? $description : PAGE_DESCRIPTION; $this->keywords = $keywords != '' ? $keywords : PAGE_KEYWORDS; if ($this->keyword != '' && strpos($this->keywords, $this->keyword) === false) { $this->keywords = $this->keyword.','.$this->keywords; } if (strpos($this->keywords, DOMAIN_NAME) === false) { $this->keywords = DOMAIN_NAME.','.$this->keywords; } if (defined('MYPHP_SITE_PACKAGE_PATH')) { $this->keywords .= ','.basename(MYPHP_SITE_PACKAGE_PATH); } $this->robots = META_ROBOTS; $this->copyright = @TEXT_COPYRIGHT_BY.' '.META_COPYRIGHT_TEXT; $this->author = @TEXT_CREATED_BY.' '.META_AUTHOR_TEXT; $this->Content_Type = META_CONTENT_TYPE; $this->Content_Script_Type = META_CONTENT_SCRIPT_TYPE; $this->Content_Language = $language != ''?$language:PAGE_LANGUAGE; if (defined('CACHE_BROWSER') && CACHE_BROWSER) { // Normal } else { $filename = ''; $lineno = ''; if (headers_sent($filename, $lineno)) { // TODO move to another class and add to head.php } else { if (defined('CACHE_BROWSER') && CACHE_BROWSER) { // Do nothing } else { header("Expires: mon, 11 mar 1953 18:55:00"); // Date in the past header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); // Always modified header("Cache-Control: no-cache, must-revalidate"); // HTTP 1.1 // Or use this, if you are using a form and using back button // header("Cache-control: private"); // Regarding SSL/IE bug/Sessions, // also make sure you DO NOT SET THE HEADER 'Pragma: no-cache' // if you are sending an inline document (e.g., PDF document). // TODO if (! 'SSL') { } header("Pragma: no-cache"); // HTTP 1.0 } } $this->Expires = '-1'; $this->Cache_Control = 'no-cache'; $this->Pragma = 'no-cache'; } // IE specific $this->Site_Enter = META_SITE_ENTER; $this->Site_Exit = META_SITE_EXIT; $this->Page_Enter = META_PAGE_ENTER; $this->Page_Exit = META_PAGE_EXIT; $this->resource_type = META_RESOURCE_TYPE; $this->distribution = META_DISTRIBUTION; $this->revisit_after = META_REVISIT_AFTER; } /** * Get html * Returns the meta data for the page * @return String the complete html */ function getHtml() { $html = $this->html; if (defined('HTTP_USER_AGENT') && HTTP_USER_AGENT!=HTTP_USER_AGENT_P900) { if ($this->keyword != '') { $html .= "<!-- ".$this->getClassName().' keyword: '.$this->keyword." -->\r\n"; // TODO what? } $html .= $this->getContent('title' , META_SHOW_CONTENT_TITLE); $html .= $this->getContent('description' , META_SHOW_CONTENT_DESCRIPTION); $html .= $this->getContent('keywords' , META_SHOW_CONTENT_KEYWORDS); $html .= $this->getContent('robots' , META_SHOW_CONTENT_ROBOTS); $html .= $this->getContent('copyright' , META_SHOW_CONTENT_COPYRIGHT); $html .= $this->getContent('author' , META_SHOW_CONTENT_AUTHOR); $html .= $this->getContent('resource_type', META_SHOW_CONTENT_RESOURCE_TYPE); $html .= $this->getContent('distribution' , META_SHOW_CONTENT_DISTRIBUTION); $html .= $this->getContent('revisit_after', META_SHOW_CONTENT_REVISIT_AFTER); // IE Specific $html .= $this->getHttpEquiv('Site_Enter' , META_SHOW_HTTP_EQUIV_SITE_ENTER); $html .= $this->getHttpEquiv('Site_Exit' , META_SHOW_HTTP_EQUIV_SITE_EXIT); $html .= $this->getHttpEquiv('Page_Enter' , META_SHOW_HTTP_EQUIV_PAGE_ENTER); $html .= $this->getHttpEquiv('Page_Exit' , META_SHOW_HTTP_EQUIV_PAGE_EXIT); $html .= $this->getHttpEquiv('keywords' , META_SHOW_HTTP_EQUIV_KEYWORDS); $html .= $this->getHttpEquiv('Content_Script_Type', META_SHOW_HTTP_EQUIV_SCRIPT_TYPE); $html .= $this->getHttpEquiv('Content_Type', META_SHOW_HTTP_EQUIV_CONTENT_TYPE); $html .= $this->getHttpEquiv('Content_Language', META_SHOW_HTTP_EQUIV_LANGUAGE); $html .= $this->getHttpEquiv('Expires' , META_SHOW_HTTP_EQUIV_EXPIRES); $html .= $this->getHttpEquiv('Cache_Control', META_SHOW_HTTP_EQUIV_CACHE_CONTROL); $html .= $this->getHttpEquiv('Pragma' , META_SHOW_HTTP_EQUIV_PRAGMA); if (defined('JAVASCRIPT_INCLUDE') && JAVASCRIPT_INCLUDE & JAVASCRIPT_INCLUDE_WIDG_EDITOR) { //$html .= '<meta name="MSSmartTagsPreventParsing" content="true" />'."\r\n"; } } return $html; } /** * Set the Meta Content. * @param String $name The name of the meta content tag * @param String $value The value of the content attribute * @param String $show Show the Meta tag or not the meta tag */ function setContent($name, $value, $show) { if (defined('META_SHOW_CONTENT') && META_SHOW_CONTENT & $show) { $this->$name = $value; } } /** * Get Meta name Content. * Returns the html for the meta name/content * @param String $name The name of the meta tag * @param String $show Show the Meta tag or not the meta tag * @return String the complete html */ function getContent($name, $show) { $html = ''; if (defined('META_SHOW_CONTENT') && META_SHOW_CONTENT & $show) { if (isset($this->$name) && $this->$name != '') { $html .= '<meta name="'.$name.'" content="'.$this->$name.'" />'."\r\n"; } } return $html; } /** * Set the Meta http-equiv Content. * @param String $name The name of the meta setHttpEquiv tag * @param String $value The value of the content attribute * @param String $show Set the Meta tag or not */ function setHttpEquiv($name, $value, $show) { if (defined('META_SHOW_HTTP_EQUIV') && META_SHOW_HTTP_EQUIV & $show) { $this->$name = $value; } } /** * Get Meta http-equiv Content. * Get the html for the meta http-equiv/content * Any '_' in the $name, are automatically replaced with '-' * @param String $name The name of the meta tag * @param String $show Show the Meta tag or not the meta tag * @return String The complete html */ function getHttpEquiv($name, $show) { $html = ''; if (defined('META_SHOW_HTTP_EQUIV') && META_SHOW_HTTP_EQUIV & $show) { if (isset($this->$name) && $this->$name != '') { $html .= '<meta http-equiv="'.str_replace('_','-', $name).'" content="'.$this->$name.'" />'."\r\n"; } } return $html; } /** * Display html * <code> * Usage: * Meta::display($title, $description, $keyword, $keywords, $language); * </code> * @static * @param String $title The title of the page * @param String $description The description of the page * @param String $keyword The unique keyword for this page * @param String $keywords The keywords for the page * @param String $language The language for the page */ public static function display($title='', $description='', $keyword='', $keywords='', $language='') { $html = new Meta($title, $description, $keyword, $keywords, $language); $html->addHtml(); }}?>
Den fulde HTML kildekode for Meta klassen
<? <!-- DEBUG: Meta --> <!-- Meta keyword: Meta --> <meta name="title" content="Meta (+) finnrasmussen.dk" /> <meta name="description" content="Meta" /> <meta name="keywords" content="finnrasmussen.dk,Meta" /> <meta name="robots" content="ALL" /> <meta name="copyright" content="Copyright (c) http://Finn-Rasmussen.com/copyright/" /> <meta name="author" content="Designet af http://Finn-Rasmussen.com/" /> <meta name="resource_type" content="document" /> <meta name="distribution" content="global" /> <meta name="revisit_after" content="2 days" /> <meta http-equiv="keywords" content="finnrasmussen.dk,Meta" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Language" content="da" /> ?>
Her er 'klasse metoderne' for Meta klassen:
Her er 'objekt variable' for Meta klassen: