Scripts

HTML Helper

Met deze html helper genereert u makkelijk uw html tags. De uitleg staat in de class zelf, en in index.php is een voorbeeld pagina gegeven, waar gebruik word gemaakt van alle tags van de helper. Nu biedt de helper ondersteuning aan de volgende tags: - Document type - Meta tags (inclusief title tag) - Link tags (hiermee word de tag in de head van de pagina bedoelt, en niet de tag) - Anchor tags - Image tags - BR tags (enters) - Heading tags (h1,h2,h3 enz) - Paragrafen - Diversen - Ongeordende, geordende en defenitie lijsten Later zullen meer tags volgen. Opmerkingen altijd welkom. Veranderingen : - Broncode van index.php toegevoegd - \n variabele veranderd in PHP_EOL - var veranderd in private - public voor function toegevoegd - '' en ' worden nu veranderd in " en ' - het stukje code voor de overige attributen staat nu in een functie

index.php
<?php
require_once('html_helper.php');
$html = new HtmlHelper();

echo $html->doc_type();

echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n";

echo $html->meta('title', 'HTML Helper Class');
echo $html->meta('description', 'PHPHulp HTML Helper Script');

echo $html->link_tag('css', 'style.css');

echo "</head>\n<body>\n";

echo $html->heading('HTML Helper');

echo $html->par('welkom', 'Welkom op de test pagina van de html helper.');

echo $html->par();
echo $html->link('http://google.com/', 'Google', 'De meest geadvenceerde zoekmachine ter wereld', array('id' => 'searchlink'));
echo $html->par(null, null, null, true);

echo $html->div();
echo $html->img('http://static.php.net/www.php.net/images/php.gif');
echo $html->div(null, null, null, true);

$family_guy = array
  (
  "Griffin"=> array
    (
        "Peter",
        "Lois",
        "Megan"
    ),
  "Quagmire" => array
    (
        "Glenn"
    ),
  "Brown" => array
    (
        "Cleveland",
        "Loretta",
        "Junior"
    )
  );
echo $html->listing($family_guy, 'ul');

$defenitions = array(
                'PHP' => 'Een scripttaal, die bedoeld is om op webservers dynamische webpagina\'s te creëren.',
                'CSS' => 'CSS is een manier om de vormgeving en presentatie voor webpagina\'s middels eenvoudige code te omschrijven die de internetbrowser begrijpt en toepast.',
                'HTML' => 'Een opmaaktaal voor de specificatie van documenten, voornamelijk bedoeld voor het World Wide Web.');

echo $html->listing($defenitions, 'dl');

echo "</body>\n</html>\n";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML Helper Class</title><meta name="description" content="PHPHulp HTML Helper Script" />
<link type="text/css" href="style.css" />
</head>
<body>
<h1>HTML Helper</h1>
<p class="welkom">Welkom op de test pagina van de html helper.</p>
<p>
<a href="http://google.com/" title="De meest geadvenceerde zoekmachine ter wereld" id="searchlink">Google</a>
</p><div>
<img src="http://static.php.net/www.php.net/images/php.gif" alt="" />

</div><ul>
<li>Griffin<ul>
<li>Peter</li>
<li>Lois</li>
<li>Megan</li>
</ul>
</li>
<li>Quagmire<ul>
<li>Glenn</li>
</ul>
</li>

<li>Brown<ul>
<li>Cleveland</li>
<li>Loretta</li>
<li>Junior</li>
</ul>
</li>
</ul>
<dl>
<dt>PHP</dt><dd>Een scripttaal, die bedoeld is om op webservers dynamische webpagina's te creëren.</dd>
<dt>CSS</dt><dd>CSS is een manier om de vormgeving en presentatie voor webpagina's middels eenvoudige code te omschrijven die de internetbrowser begrijpt en toepast.</dd>

<dt>HTML</dt><dd>Een opmaaktaal voor de specificatie van documenten, voornamelijk bedoeld voor het World Wide Web.</dd>
</dl>
</body>
</html>
html_helper.php
<?php

/**
 * HTMLHelper
 * 
 * Genereer gemakkelijk html tags.
 * 
 * @author Bart Dieterman
 * @category Helpers
 */

class HtmlHelper {
    
    /**
     * Mogelijke doctypes
     *
     * @var array
     * @access private
     */
	private $__documentTypes = array(
        'html5' => '<!DOCTYPE HTML>',
		'html4_strict'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
		'html4_trans'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
		'html4_frame'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
		'xhtml_strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
		'xhtml_trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
		'xhtml_frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
		'xhtml_11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
	);
    
    /**
     * Verschillende types om naar te linken in de head
     *
     * @var array
     * @access private
     */
   	private $__linkTypes = array(
        'rss' => 'application/rss+xml',
		'css'  => 'text/css',
        'icon' => 'image/x-icon',
        'atom' => 'application/atom+xml'
	);
    
    /**
     * Mogelijkheden voor http-equiv in een meta tag
     *
     * @var array
     * @access private
     */
	private $__httpEquivs = array(
        'allow',
        'content-encoding',
        'content-language',
        'content-length',
        'content-type',
        'date',
        'expires',
        'last-modified',
        'location',
        'refresh',
        'set-cookie',
        'www-authenticate'
	);

    /**
     * HtmlHelper::documentType()
     * 
     * Weergeeft een doctype
     *
     * Mogelijke doctypes:
     *
     *  - html5 : HTML5
     *  - html4_strict:  HTML4 Strict.
     *  - html4_trans:  HTML4 Transitional.
     *  - html4_frame:  HTML4 Frameset.
     *  - xhtml_strict: XHTML1 Strict.
     *  - xhtml_trans: XHTML1 Transitional.
     *  - xhtml_frame: XHTML1 Frameset.
     *  - xhtml_11: XHTML1.1.
     *
     * Standaard word xhtml-strict gebruikt
     * 
     * @param string    $type Doctype te gebruiken
     * @return string
     * @access public
     */
	public function doc_type($type = 'xhtml_strict') {
	   return $this->__documentTypes[$type].PHP_EOL;
	}
    
    /**
     * HtmlHelper::meta()
     * 
     * Weergeeft een meta tag.
     *
     * Hiermee kan bovendien ook de titel van de pagina weergegeven worden, gebruik $type = 'title'
     * 
     * Bij de volgende types word http-equiv gebruikt:
     * - allow
     * - content-encoding
     * - content-language
     * - content-length
     * - content-type
     * - date
     * - expires
     * - last-modified
     * - location
     * - refresh
     * - set-cookie
     * - www-authenticate
     * 
     * @param string    $type Type meta data
     * @param string    $content De content van de meta tag
     * @param mixed  $options   Overige html attributen die in de link worden geplaatst
     * @return string
     * @access public
     */
    public function meta($type, $content, $options = '') {
        
        if ($type === 'title') {
            $meta = '<title>'.$content.'</title>';
            return $meta;
        }
        
        $meta = '<meta ';
        
        if (in_array($type, $this->__httpEquivs)) {
            $meta .= 'http-equivs="'.$type.'"';
        } else {
            $meta .= 'name="'.$type.'" ';
        }
        
        $meta .= 'content="'.$content.'" ';
        
        $meta .= $this->options($options);
        
        $meta .= '/>';
        
        return $meta.PHP_EOL;
        
    }
    
    /**
     * HtmlHelper::link_tag()
     * 
     * Genereert een <link> of <script> naar een bestand.
     *
     * U kunt bij type de volgende afkortingen gebruiken:
     * - rss : Een link naar een rss bestand
     * - javascript : Een script tag met $url als src
     * - icon : Een link naar uw favicon
     * - atom : Een link naar uw atom feed
     * 
     * @param string $type      De type link
     * @param string $url       De url van het te linken bestandt
     * @param string $rel       De relaltie met de url
     * @param string $media     Het medium waar de link voor geld
     * @param mixed  $options   Overige html attributen die in de link worden geplaatst
     * @acces public
     * @return string
     */
    public function link_tag($type, $url, $rel = null, $media = null, $options = '') {
        if ($type === 'javascript') {
            $link_tag = '<script type="text/javascript" src="'.$url.'"';
            if (is_array($options)) {
                foreach ($options as $key => $value) {
                    $link_tag .= ' '.$key.'="'.$value.'"';
                }
            }
            $link_tag .= '></script>';
        } else {
            if (isset($type)) {
                if (isset($this->__linkTypes[$type])) {
                    $link_tag = '<link type="'.$this->__linkTypes[$type].'" href="'.$url.'"';
                } else {
                    $link_tag = '<link type="'.$type.'" href="'.$url.'"';
                }
            } else {
                $link_tag = '<link href="'.$url.'"';
            }
            
            if (isset($rel)) {
                $link_tag .= ' rel="'.$rel.'"';
            }
            
            if (isset($media)) {
                $link_tag .= ' media="'.$media.'"';
            }
            
            $link_tag .= $this->options($options);
            
            $link_tag .= ' />';
        }
        return $link_tag.PHP_EOL;
    }
    
    /**
     * HtmlHelper::link()
     * 
     * Genereert een html anchor tag naar de opgegeven url
     *
     * Wanneer er geen text is opgegeven, word de titel gebruikt
     * Wanneer er geen titel is opgegeven, word de text gebruikt
     * 
     * @param string $url       De url waar naatoe word gelinkt
     * @param string $text      De text die tussen <a> en </a> word geplaatst
     * @param string $title     De titel van de link
     * @param mixed  $options   Overige html attributen die in de link worden geplaatst
     * @acces public
     * @return string
     */
    public function link($url, $text = null, $title = null, $options = '') {
    
        $link = '<a ';
    
        if (!isset($text)) {
            $text = $url;
        }
    
        if (!isset($title)) {
            if (isset($options['title'])) {
                $title = $options['title'];
            } else {
                $title = $text;
            }
            
        }
        $title = $this->replace_quotes($title);
        
    
        $link .= 'href="'.$url.'" title="'.$title.'"';
    
        $link .= $this->options($options);
    
        $link .= '>'.$text.'</a>';
    

    
        return $link.PHP_EOL;
 
    }
    
    /**
     * HtmlHelper::img()
     * 
     * Genereert een afbeelding
     *
     * Als er geen alt is opgegeven, word een alt="" gebruikt, omdat het een vereiste is van XHTML scrict
     * 
     * @param string $path      Het pad naar de afbeelding
     * @param string $alt       Wat getoont word, als de afbeelding niet geladen kan worden
     * @param mixed  $options   Overige html attributen
     * @acces public
     * @return string
     */
    public function img($path, $alt = null, $options = '') {
        $img = '<img ';
        
        $img .= 'src="'.$path.'" ';
        
        if (!isset($alt)) {
            if (!isset($options['alt'])) {
                $alt = '';
            } else {
                $alt = $options['alt'];
            } 
        }
        
        $alt = $this->replace_quotes($alt);
        
        $img .= 'alt="'.$alt.'"';
        
        $img .= $this->options($options);
        
        $img .= ' />';
        
        return $img.PHP_EOL;      
        
    }
    
    /**
     * HtmlHelper::br()
     * 
     * Genereert 1 of meerdere <br /> tags
     * 
     * @param string $num      Het aantal keer dat de tag herhaalt moet worden
     * @acces public
     * @return string
     */
	public function br($num = 1){
		return str_repeat('<br />', $num);
	}

    /**
     * HtmlHelper::heading()
     * 
     * Genereert een heading tag, standaar <h1>
     * 
     * @param string $text  Wat tussen de <h1> en </h1> tags komt te staan
     * @param string $head  De groote van de heading
     * @acces public
     * @return string
     */
    public function heading($text, $head = 1) {
        return '<h'.$head.'>'.$text.'</h'.$head.'>'.PHP_EOL;
    }
    
    /**
     * HtmlHelper::par()
     * 
     * Genereert een paragraaf. Als er geen text is opgegeven, alleen <p> of </p>
     * 
     * @param string $class    De class van de paragraaf 
     * @param string $text  
     * @param string $options  Overige html attributen
     * @param bolean $closetag Als er geen text is opgegeven, en dit is true, word </p> gereturnd
     * @acces public
     * @return string
     */
    public function par($class = null, $text = null, $options = '', $closetag = false) {
        if (!isset($text)) {
            if ($closetag) {
                return '</p>';
            }
        }
        $par = '<p';
        
        if (isset($class)) {
            $class = $this->replace_quotes($class);
            $par .= ' class="'.$class.'"';
        }
        
        $par .= $this->options($options);
        
        if (isset($text)) {
            $par .= '>'.$text.'</p>';
        } else {
            $par .= '>';
        }
        
        return $par.PHP_EOL;
        
    }
    
    /**
     * HtmlHelper::par()
     * 
     * Genereert een div. Als er geen text is opgegeven, alleen <div> of </div>
     * 
     * @param string $class    De class van de div 
     * @param string $text  
     * @param string $options  Overige html attributen
     * @param bolean $closetag Als er geen text is opgegeven, en dit is true, word </div> gereturnd
     * @acces public
     * @return string
     */
    public function div($class = null, $text = null, $options = '', $closetag = false) {
        if (!isset($text)) {
            if ($closetag) {
                return '</div>';
            }
        }
        $div = '<div';
        
        if (isset($class)) {
            $class = $this->replace_quotes($options);
            $div .= ' class="'.$class.'"';
        }
        
        $div .= $this->options($options);
        
        if (isset($text)) {
            $div .= '>'.$text.'</div>';
        } else {
            $div .= '>';
        }
        
        return $div.PHP_EOL;
        
    }
    
    /**
     * HtmlHelper::listing()
     * 
     * Genereert een georderde lijst (ol), ongeorderde lijst (ul) of een defenitie lijst (dl)
     * 
     * Met een ol en ul is het mogelijk om een multidimensional arrays in te voeren.
     * Bij een dl dient een associative array ingevoerd te worden.
     * 
     * @param string $data  Een array
     * @param string $type  UL, OL of DL. UL is standaard
     * @acces public
     * @return string
     */
    public function listing($data, $type = 'ul')
    {
        $recursion = __FUNCTION__;

        $listing = '<'.$type.'>'.PHP_EOL;
        
        if ($type === 'dl') {
            foreach ($data as $dt => $dd) {
                $listing .= '<dt>'.$dt.'</dt><dd>'.$dd.'</dd>'.PHP_EOL;
            }
        } else {
            if (is_array($data)) {
                foreach ($data as $key => $elem) {
                    if (is_array($elem)) {
                        $listing .= '<li>'.$key.$this->$recursion($elem, $type).'</li>'.PHP_EOL;
                    } else {
                        $listing .= '<li>'.$elem.'</li>'.PHP_EOL;
                    }
                }
            } else {
                $listing .= '<li>'.$data.'</li>'.PHP_EOL;
            }
        }
        $listing .= '</'.$type.'>'.PHP_EOL;
              
        return $listing;
        
    }
    
    /**
     * HtmlHelper::options()
     * 
     * Genereert de overige atributen van een tag
     * 
     * @param mixed $options
     * @acces private
     * @return string
     */
    private function options($options) {
        if (is_array($options)) {
            foreach ($options as $key => $value) {
                $value = $this->replace_quotes($value);
                
                $out = ' '.$key.'="'.$value.'"';
                return $out;
            }
        }
    }
    
    /**
     * HtmlHelper::replace_quotes()
     * 
     * Vervangt ' door &#39; en " door &#34;
     * 
     * @param string $string
     * @acces private
     * @return string
     */
    private function replace_quotes($string) {
        $string = str_replace("'", "&#39;", $string);
        $string = str_replace('"', "&#34;", $string);
        
        return $string;
    }
    
}

/* Einde van html_helper.php */

Reacties

0
Nog geen reacties.