Posted By


claudiowebdesign on 10/21/11

Tagged


Statistics


Viewed 695 times
Favorited by 0 user(s)

Document.class.php


/ Published in: PHP
Save to your folder(s)

This is a little PHP class that permits to start quickly the creation of a webpage.

When you declare an instance of the class it automatically starts the output buffering and gzip compression, it outputs all the proper tags (doctype, meta, stylesheets, scripts, etc.) until the .

Everything you write after that gets actually placed inside the body, and when the page ends the and tags gets automatically added at the bottom.

In addition to this, the class gets your IP so to display all the possible error/warning messages to you and none to the random users.

EXAMPLES/USAGE: http://claudiobonifazi.com/snippets/Document


Copy this code and paste it in your HTML
  1. <?php
  2. /* --------------------------------------------------------------------
  3.   * Document.class.php - PHP class to quick start web page development
  4.   * Distributed under the Do-wathever-the-hell-you-want-with-it License
  5.   *
  6.   * Web site: http://claudiobonifazi.com
  7.   * Blog: http://claudiobonifazi.com?p=4
  8.   * Email: [email protected]
  9.   * Twitter: @ClaudioBonifazi
  10.   * -------------------------------------------------------------------
  11.   *
  12.   * Start any webpage this way:
  13.   *
  14.   * <?php
  15.   * include '/path/Document.class.php';
  16.   * $html = new Document (
  17.   * 'title' => 'The page title tag',
  18.   * 'description' => 'description tag',
  19.   * 'keywords' => 'keywords tag',
  20.   * 'author' => 'author tag',
  21.   * 'language' => 'en/de/fr/it/sp/whatever',
  22.   * 'charset' => 'utf-8/whatever',
  23.   * 'favicon' => 'faviconFileName.ico',
  24.   * 'stylesheets' => array( 'reset.css'=>'all', 'main.css'=>'all', 'another.css'=>'media' ),
  25.   * 'ieFallback' => 'theCssFileReservedForOldBrowsers.css',
  26.   * 'googleFonts' => array('that cool font i saw', 'that other one', 'great Scott!'),
  27.   * 'scripts' => array( 'jQuery.js', 'head.js', 'my.js', 'whatever.js' )
  28.   * );
  29.   * ?>
  30.   * You can leave blank anyone of the parameters, they all have a default value
  31.   * Anything written after this will be placed inside the <body> tag
  32.   * The closure tags will be automatically added.
  33.   *
  34.   */
  35.  
  36. class Document{
  37.  
  38. private $startTime;
  39. private $adminIP = '127.0.0.1';
  40.  
  41. const FAVICONFOLDER = './';
  42. const CSSFOLDER = './components/css/';
  43. const JSFOLDER = './components/js/';
  44. private $head = array(
  45. 'title' => 'Untitled Document',
  46. 'description' => '',
  47. 'keywords' => '',
  48. 'author' => '',
  49. 'language' => 'en',
  50. 'charset' => 'utf-8',
  51. 'favicon' => '',
  52. 'stylesheets' => array(),
  53. 'ieFallback' => '',
  54. 'googleFonts' => array(),
  55. 'scripts' => array()
  56. );
  57.  
  58.  
  59. public function __construct( $headOverride = array() ){
  60.  
  61. // this can be a solution if you can't set error reporting on php.ini or the .htaccess file
  62. // error outputs get printed only to you ( if you properly set the $adminIP property )
  63. if ( !empty($_SERVER['HTTP_CLIENT_IP']) ){
  64. $ip = $_SERVER['HTTP_CLIENT_IP'];
  65. }else{
  66. if( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) )
  67. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  68. else
  69. $ip = $_SERVER['REMOTE_ADDR'];
  70. }
  71. if( $ip == $this->adminIP ){
  72. ini_set( 'display_errors', 'On' );
  73. error_reporting( E_ALL | E_STRICT );
  74. }else{
  75. ini_set( 'display_errors', 0 );
  76. error_reporting( NULL );
  77. }
  78.  
  79. // output buffering via script (if not set by server settings)
  80. ob_start ("ob_gzhandler");
  81.  
  82. // storing the script execution start time could be useful
  83. $this->startTime = time();
  84.  
  85. //data management
  86. $head = $this->head;
  87. foreach( $headOverride as $tag=>$val ){
  88. if( !isset($head[$tag]) ){
  89. trigger_error("There's no point in adding $tag head property, it won't be considered. Maybe you mispelled it?");
  90. unset($headOverride[$tag]);
  91. }else{
  92. if( $head[$tag]!=$headOverride )
  93. $head[$tag] = $headOverride[$tag];
  94. }
  95. }
  96.  
  97.  
  98. // starting tags
  99. echo "<!doctype html>",
  100. "\n<html lang=\"$head[language]\">",
  101. "\n\t<head>";
  102.  
  103. // initial meta tags
  104. echo "\n\t\t<title>$head[title]</title>",
  105. "\n\t\t<meta name=\"description\" content=\"$head[description]\">",
  106. "\n\t\t<meta name=\"keyword\" content=\"$head[keywords]\">",
  107. "\n\t\t<meta name=\"author\" content=\"$head[author]\">",
  108. "\n\t\t<meta name=\"language\" content=\"$head[language]\">",
  109. "\n\t\t<meta name=\"charset\" content=\"$head[charset]\">";
  110.  
  111. // google web fonts
  112. if( !empty($head['googleFonts']) )
  113. foreach( $head['googleFonts'] as $n=>$name ){
  114. echo "\n\t\t<link rel=\"stylesheet\" href=\"http://fonts.googleapis.com/css?family=".urlencode($name)."\">";
  115. }
  116.  
  117. // stylesheets
  118. foreach( $head['stylesheets'] as $file=>$media )
  119. echo "\n\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$file."\" media=\"$media\">";
  120.  
  121. // Internet Explorer fallbacks
  122. if( $head['ieFallback']!='.css' && $head['ieFallback']!='' )
  123. echo "\n\t\t<!--[if lt IE 9]>",
  124. "\n\t\t\t<script type=\"text/javascript\" src=\"http://html5shiv.googlecode.com/svn/trunk/html5.js\"></script>",
  125. "\n\t\t\t<link rel=\"stylesheet\" href=\"".Document::CSSFOLDER.$head['ieFallback']."\" media=\"screen\">",
  126. "\n\t\t<![endif]-->";
  127.  
  128. // scripts ( I suggest you to use http://headjs.com )
  129. foreach( $head['scripts'] as $n=>$file )
  130. echo "\n\t\t<script src=\"".Document::JSFOLDER.$file.'"></script>';
  131.  
  132. // favicon
  133. if( !empty($head['favicon']) )
  134. echo "\n\t\t<link rel=\"shortcut icon\" href=\"".Document::FAVICONFOLDER.$head['favicon'].'">';
  135.  
  136. // close and initialize body section
  137. echo "\n\t</head>",
  138. "\n\t<body>",
  139. "\n";
  140. }
  141.  
  142. public function __destruct(){
  143. // when execution ends those ending tags will be automatically added
  144. echo "\n\t</body>",
  145. "\n</html>";
  146. }
  147. }
  148. ?>

URL: http://claudiobonifazi.com/snippets/Document

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.