Ris/Ros af en html klasse
Jeg har forsøgt at lave en (x)html klasse, og har opbygget følgende.Er der nogen som har nogle tilføjelser, rettelser, noget som jeg bør lave anderledes eller lign.
Her er min kode:
<?php
class HTML
{
private $dtd;
private $html = '';
private $title;
private $meta = array();
private $css = array();
private $script = array();
private $endtag;
private $newline;
public function __construct($sTitle,$sDtd='xhtml',$sEncoding='utf-8',$sCss='style.css')
{
$this->setNewline();
$this->setTitle($sTitle);
$this->setDtd($sDtd);
$this->setEncoding($sEncoding);
$this->setCss($sCss);
}
public function setNewline($bool=true)
{
if($bool)
{
$this->newline = "\n";
}
else
{
$this->newline = '';
}
}
public function getNewline()
{
return $this->newline;
}
public function setCss($sCss,$sMedia='screen')
{
$output = array();
switch($sMedia)
{
case 'all': //Suitable for all devices.
$output['media'] = 'all';
break;
case 'aural': //Intended for speech synthesizers. See the section on aural style sheets for details.
$output['media'] = 'aural';
break;
case 'braille': //Intended for braille tactile feedback devices.
$output['media'] = 'braille';
break;
case 'embossed': //Intended for paged braille printers.
$output['media'] = 'embossed';
break;
case 'handheld': //Intended for handheld devices (typically small screen, monochrome, limited bandwidth).
$output['media'] = 'handheld';
break;
case 'print': //Intended for paged, opaque material and for documents viewed on screen in print preview mode. Please consult the section on paged media for information about formatting issues that are specific to paged media.
$output['media'] = 'print';
break;
case 'projection': //Intended for projected presentations, for example projectors or print to transparencies. Please consult the section on paged media for information about formatting issues that are specific to paged media.
$output['media'] = 'projection';
break;
case 'tty': //Intended for media using a fixed-pitch character grid, such as teletypes, terminals, or portable devices with limited display capabilities. Authors should not use pixel units with the "tty" media type.
$output['media'] = 'tty';
break;
case 'tv': //Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).
$output['media'] = 'tv';
break;
default:
$output['media'] = 'screen'; //Intended primarily for color computer screens.
}
$output['css'] = 'style/' . basename($sCss);
if(file_exists($output['css']))
{
$this->setMeta('<meta http-equiv="Content-Style-Type" content="text/css">');
$this->css[] = $output;
}
}
public function getCss()
{
$output = '';
if(count($this->css)>0)
{
foreach($this->css AS $thiscss)
{
$output .= '<link rel="stylesheet" type="text/css" media="' . $thiscss['media'] . '" href="' . $thiscss['css'] . '"' . $this->endtag . '>' . $this->getNewline();
}
}
return $output;
}
public function setEncoding($sEncoding)
{
$sEncoding = strtolower($sEncoding);
switch($sEncoding)
{
case 'iso-8859-1':
$this->setMeta('<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">');
break;
case 'windows-1252':
$this->setMeta('<meta http-equiv="Content-Type" content="text/html;charset=windows-1252">');
break;
default:
$this->setMeta('<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">');
}
}
public function setMeta($sMeta)
{
$this->meta[] = str_replace('>',$this->endtag . '>',$sMeta) . $this->getNewline();
}
public function getMeta()
{
$meta = array_unique($this->meta);
return implode('',$meta);
}
public function setScript($script)
{
$output = array();
$type = strtolower(end(explode('.',$script)));
switch($type)
{
case 'vbs':
$output['type'] = 'text/vbscript';
$this->setMeta('<meta http-equiv="Content-Script-Type" content="text/vbscript">');
break;
case 'js':
$output['type'] = 'text/javascript';
$this->setMeta('<meta http-equiv="Content-Script-Type" content="text/javascript">');
break;
default:
$output['type'] = 'text/tcl';
$this->setMeta('<meta http-equiv="Content-Script-Type" content="text/tcl">');
}
$output['script'] = 'script/' . basename($script);
if(file_exists($output['script']))
$this->script[] = $output;
}
public function getScript()
{
$output = '';
if(count($this->script)>0)
{
foreach($this->script AS $thisscript)
{
$output .= '<script type="' . $thisscript['type'] . '" src="' . $thisscript['script'] . '"></script>' . $this->getNewline();
}
}
return $output;
}
public function setDtd($sDtd, $sType='strict')
{
$sDtd = strtolower($sDtd);
$sType = strtolower($sType);
switch($sDtd)
{
case 'html':
$this->endtag = '';
switch($sType)
{
case 'transitional':
$this->dtd = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
break;
case 'frameset':
$this->dtd = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
break;
default:
$this->dtd = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict //EN" "http://www.w3.org/TR/html4/strict.dtd">';
}
break;
default:
$this->endtag = ' /';
switch($sType)
{
case 'transitional':
$this->dtd = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">';
break;
case 'frameset':
$this->dtd = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/frameset.dtd">';
break;
default:
$this->dtd = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">';
}
}
}
public function getDtd()
{
return $this->dtd . $this->getNewline();
}
public function setTitle($sTitle)
{
$this->title = $sTitle;
}
public function getTitle()
{
return $this->title;
}
public function setHtml($sHtml)
{
$this->html .= $sHtml . $this->getNewline();
}
public function getHtml()
{
return $this->html;
}
public function output($print=true)
{
$output = '';
$output = $this->getDtd();
$output .= '<html>' . $this->getNewline();
$output .= '<head>' . $this->getNewline();
$output .= '<title>' . $this->getTitle() . '</title>' . $this->getNewline();
$output .= $this->getMeta();
$output .= $this->getCss();
$output .= $this->getScript();
$output .= '</head>' . $this->getNewline();
$output .= '<body>' . $this->getNewline();
$output .= $this->getHtml();
$output .= '</body>' . $this->getNewline();
$output .= '</html>' . $this->getNewline();
if($print)
{
echo $output;
}
else
{
return $output;
}
}
public function debug($print=true)
{
$output = '';
$output .= '<pre>';
$output .= print_r($this,true);
$output .= '</pre>';
if($print)
echo $output;
else
return $output;
}
}
?>
