pretty source.
| From: | Hans Raber | Date: | Fri, 29 Mar 2002 11:07:48 +0000 |
| Subject: | pretty source. | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-9357@lists.php.net to get a copy of this message | ||
hello.
i've found an interesting way, how the output of php documents can be beautified in a really easy way.
as i couldn't find anything similar i thought it could have really been my idea.
please read the 100 lines and tell me wether you like the idea or not.
Pretty Print for Html PagesWe all know the problem: the output of dynamically generated pages typically looks like this: <html> <head> <title>bla bla bla</title> </head> <body> <!---- module include starts here //----> <table> ..... </table> <!---- module include ends here //------> </body> </html> and the php document looks like this: <html> <head> <title>blabla </title> </head> <body> <? if($cond) {
echo " <!--- module ...........\n";
...
echo " ....\n";
}
?>
</body>
</html>
With jsp, asp, cgi, or whatever the problem still remains the same.
But theres an easy solution to this problem: it consits of one variable and
one to two functions (depends on the language).
Here is the reference implementation for PHP:
[ mPrint.php ]
<?
$_MARGIN="";
function mPrint($str="",$margin="")
{
GLOBAL $_MARGIN;
if($margin=="LESS") $_MARGIN=substr($_MARGIN,2); // drop two chars
echo "$_MARGIN$str\n";
if($margin=="MORE") $_MARGIN=$_MARGIN." "; // add two chars} ?> The order: first look for less, then echo, then more is due to the html syntax. Have a look at this simple example and you'll understand (all echos and prints are replaced by mPrint): <? include("mPrint.php"); mPrint("<html>",MORE); mPrint("<head>",MORE); mPrint("<title>",MORE); mPrint("test"); mPrint("</title>",LESS); mPrint("</head>",LESS); mPrint("<body>",MORE); mPrint("hello, this is a test. <br>"); mPrint("have a look at the source!!!<br>"); mPrint("and i dont have to type millions of whitespaces in my php document!!!"); mPrint("</body>",LESS); mPrint("</html>",LESS); ?> The resulting html-source is the following: <html> <head> <title>
test</title> </head> <body> hello, this is a test. <br> have a look at the source!!!<br> and the php source is formated well too!!! </body> </html> But the best of all is that inserting a tag over the half of your source is done by typing: mPrint("<div style=\"padding-left:20px;\">",MORE); .... mPrint("</div>",LESS); And the whole html code is reformatted automatically, without having to reformat your source. Great, isn't it? hope i haven't stolen your time, if so please tell me too thanks, hans ps. please excuse my english.