[NTLUG:Discuss] PHP Code for Lazy HTML Coders

Jason Ferguson jferg3 at swbell.net
Mon Dec 31 08:26:35 CST 2001


I wanted to share the following PHP code I cobbled together so that if
anyone else had a use for it, they are welcome to it.

I created this code because after creating what feels like MILLIONS of
web pages over the last several years, Im sick of the first section: the
<HTML> declaration and the <HEAD> section. Most of that is pretty
repetitive, so I created a function to automate that section. Instead of
all the repetitive type for each page, all I do is start each document
like this:

(Note: for the real HTML purists, I handle the <DOCTYPE> by including a
text file with only the DOCTYPE in it. That way I can change one small
file and all my pages are updated).

<?
	include("doctype.txt")
	include("functions.inc");
	htmlheader(title, stylesheet location, meta tag stuff);
?>
 

Anyhow, hope someone finds it of use. I tried to comment it pretty well
in case something breaks

Jason


<?

function htmlheader()
{
// Written by Jason Ferguson, (c) 2001
//
// Function to generate the header section of the html document. We've
already taken care of the DOCTYPE before this.
// example of function use:
//
// 	htmlheader("My Page","style.css","NAME","keywords","CONTENT","Jason,
misc stuff")
//
// would generate these lines:
//
// <html>
// <head>
// <title>My Page</title>
// <LINK REL="Stylesheet" HREF="style.css">
// <META NAME="keywords" CONTENT="Jason, misc stuff">
// </head>
//
// version info:
// 0.1: It works for <TITLE> and <META>, but forgot to do style sheets!
doh!
// 0.2: Okay, okay, I added the <LINK> tag for stylesheets. Relative and
absolute URLs to the stylesheet should work. Just put "" if there is no
stylesheet

	$j=func_num_args();   // first, we get the number of arguments
	$arg_list=func_get_args();
	
	// next, we take the first argument and make it the html title
	if ($j>=1)
	{
		echo "<html>\n";
		echo "<head>\n";
		echo "<title>\n";
		echo $arg_list[0];
		echo "</title>\n";
	}
	
	// next, lets link in the stylesheet

	if ($j>=2)
	{
		echo "<LINK REL=\"Stylesheet\" HREF=\"";
		echo $arg_list[1];
		echo "\">\n";
	}

	// next, if there are more arguments, they are assumed to be for META
tags. Lord knows, I never use the things, but still.

	if ($j>1)
	{
		$j--;     //subtract one from the number of arguments. This accounts
for the first argument, which is the title for the document.
		$j--;     // Do it again to account for the stylesheet

		// if the number of arguments is evenly divisable by four, the
function is "clean", so we create the META tag
		// There is no checking (yet) of the first and third arguments to make
sure they are correct.

		if ((j%4)==0)
		{
			for ($i=2; $i < func_num_args(); $i++)
			{
				echo "<META ";
				$content=$arg_list[$i];

				echo $content;
				$i++;
				$content=$arg_list[$i];
		
				echo "=\"$content\"";
				$i++;
				$content=$arg_list[$i];
				echo " $content=\"";
				$i++;
				$content=$arg_list[$i];
				echo "$content\">\n";
			}
		}
		// if the number is NOT evenly divisable by four, we cant generate the
META tag, so we skip it
		else
		{
			echo "Attempted to call header function with invalid number of
arguments. Number of arguments must be (args*4)+1.\n";
		}
	}
	echo "</head>\n";
}



?>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://ntlug.org/pipermail/discuss/attachments/20011231/bc750177/attachment.bin


More information about the Discuss mailing list