Welcome Guest, Not a member yet? Register   Sign In
Doctype Plugin
#1

[eluser]Adam Griffiths[/eluser]
I recently made this plugin to display a doctype easily. This should be saved as doctype_pi.php in your application/plugins/ folder and loaded like...

Code:
$this->load->plugin('doctype');

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Doctype
*
* Generates a doctype based on the strings passed to the function.
* Defaults to xHTML Strict
*
* @access    public
* @param    string
* @param    string
* @return    string
*/

function doctype($lang = 'xhtml', $type = 'strict')
{    
    if($lang === NULL)
    {
        exit("No language sent to HTML Helper doctype()");
    }
    elseif($lang === 'xhtml')
    {
        if($type === 'strict')
        {
            echo("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
        }
        elseif($type === 'transitional')
        {
            echo("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
        }
        elseif($type = 'frameset')
        {
            echo("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">\n");
        }
        else
        {
            show_error("Never be seen. Sshhhhhhh!");
        }
    }
    elseif($lang === 'html')
    {
        if($type === 'strict')
        {
            echo("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
        }
        elseif($type === 'transitional')
        {
            echo("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
        }
        elseif($type = 'frameset')
        {
            echo("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n");
        }
        else
        {
            show_error("Never be seen. Sshhhhhhh!");
        }
    }
    else
    {
        show_error("Never be seen. Sshhhhhhh!");
    }
    
} // doctype

/* End of file doctype_pi.php */
/* Location: application/helpers/doctype_pi.php */

Usage
You can display both HTML and xHTML doctpyes using this funtion. Pass the HTML type to the first function, and the doctype you want to use (strict, transitional, frameset).

The default will be xHTML Strict.

It's small, but it sure does help me a lot! It's easier to type out doctype(); than <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">.

It's probably not the best I could've done, I could've done it differently. But it works and is quite useful to myself, so I thought I'd share.

Thanks.
#2

[eluser]Derek Allard[/eluser]
Love it, thanks for sharing.
#3

[eluser]Randy Casburn[/eluser]
Sweet man! Nice. One of those "why didn't I think of that!

Thanks Adam.

Randy
#4

[eluser]xwero[/eluser]
Personally i like the syntax of OP in this thread more. Because it leaves the door open for other doctypes. And why not use a config file for the different doctypes?
Code:
// config (config/doctypes.php)
$config['xhtml-strict'] = 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"';
$config['xhtml-transitional'] = 'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"';
$config['xhtml-frameset'] = 'html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"';
$config['html-strict'] = 'HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"';
$config['html-transitional'] = 'HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"';
$config['html-frameset'] = 'HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"';
// plugin
function doctype($key)
{
    $CI=& get_instance();
    $CI->load->config('doctypes');

    return ( ! $CI->config->item($key))? $key: '<!DOCTYPE '.$CI->config->item($key).">\n";
}
It also gives other developers a clearer view on what the doctype is. If you add doctype() to the view they have to check the plugin to see which is the default doctype. If you add doctype('xhtml-strict') they know right away.
#5

[eluser]Adam Griffiths[/eluser]
Thanks guys.

@xwero: You don't need to just put doctype();, by all means if it helps you can put doctype('xhtml', 'strict'); I didn't use a config file because I don't think it warrants one, it's just a small function and is by no means a be all and end all solution for everyone.

It serves a purpose and if any newer doctpyes are released, I will update it. :-)
#6

[eluser]xwero[/eluser]
Now the people have two ways of adding the doctype. Personally i don't like your error messages they don't belong in a professional environment.

There are more doctype than you added and you don't know which doctype someone needs so instead of updating it yourself or let a developer alter the plugin himself putting the doctypes in a config file is the most flexible way to generate the html output.

Also you code is too complex for such a easy generation. If another $lang is needed it requires another elseif around line 30. Keep your code as compact and flexible as possible that is what i'm trying to point out.
#7

[eluser]Adam Griffiths[/eluser]
Ok, so I made another version, it's more like yours but you don't need to echo the function.

doctype_pi.php

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Doctype
*
* Generates a doctype based on the strings passed to the function.
* Defaults to xHTML Strict
*
* @access    public
* @param    string
* @return    string
*/

function doctype($doctype)
{
    $CI =& get_instance();
    $CI->config->load('doctypes');
    
    echo $CI->config->item($doctype);
    
} // doctype

/* End of file doctype_i.php */
/* Location: application/helpers/doctype_pi.php */

config/doctypes.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['xhtml-strict'] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";

$config['xhtml-transitional'] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";

$config['xhtml-frameset'] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">\n";

$config['html-strict'] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";

$config['html-transitional'] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";

$config['html-frameset'] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n";

/* End of file doctypes.php */
/* Location: application/config/doctypes.php */

Usage
Code:
&lt;?php doctype('xhtml-strict'); ?&gt;

or

Code:
&lt;?=doctype('xhtml-strict');?&gt;

As before, xHTML and HTML Strict, Tranisitional and Frameset are supported.

Oh and @my error messages, there is no way on earth that they would be shown to the user in the file I gave, it was just a bit of fun. I used it in a project with another developer and it made us both laugh. It was just a quick knockup of a function we needed to have.
#8

[eluser]axpen[/eluser]
Great idea, I was wondering if anyone did this with CodeIgniter, good job.

Also just a note for people that don't already know, just a doctype alone doesn't make a valid XHTML page, it must come after &lt;?xml version="1.0" encoding="iso-8859-1"?&gt; or such and followed by a &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;.




Theme © iAndrew 2016 - Forum software by © MyBB