DTP


 
Lively discussions on the graphic arts and publishing — in print or on the web


Go Back   Desktop Publishing Forum > General Discussions > Web Site Building & Maintenance

Reply
 
Thread Tools Display Modes
Old 02-18-2008, 05:29 AM   #1
CarlSeiler
Member
 
CarlSeiler's Avatar
 
Join Date: Oct 2005
Location: Denton, TX
Posts: 271
Default PHP/HTML Pretty Printer

I'm surprised I haven't been able to find such an animal. What I'm looking for is something that pretty prints php but will also clean up (X)HTML. While I realize it may be a challenge for the embedded HTML inside the php, I'm thinking it wouldn't be all that difficult for a formatter to clean up HTML that's outside of the php tags.

My problem is that I can't seem to find something that does both. I've found HTML pretty printers that either ignore the php sections or place it into a long single line. I've also found php pretty printers that ignore the HTML or mangle it--for example, TIDY mangles everything because there's no header (it's created with a php include).

One solution might be to find two separate tools that do the least amount of damage, then run the file through in sequence. I've also considered cutting and pasting.

Luckily most of my code is readable--at least to me--but sometimes I just wish things could be straightened up a bit.

Carl
CarlSeiler is offline   Reply With Quote
Old 02-18-2008, 11:53 PM   #2
iamback
Member
 
iamback's Avatar
 
Join Date: Oct 2005
Location: Amsterdam, NL
Posts: 4,894
Default

Quote:
Originally Posted by CarlSeiler View Post
I'm surprised I haven't been able to find such an animal. What I'm looking for is something that pretty prints php but will also clean up (X)HTML. While I realize it may be a challenge for the embedded HTML inside the php, I'm thinking it wouldn't be all that difficult for a formatter to clean up HTML that's outside of the php tags.

My problem is that I can't seem to find something that does both.
I think that "pretty printers" may have gone out of fashion, actually.

The idea of course is to create "pretty" code off the bat, and decent text editors will help you these days with auto-indent features (even language-savvy auto-indent), so there's hardly any excuse left (for a human) to not write code that's well-indented. The other cause may be that there are many possible "rules" for indenting code (and software projects may impose theirs), which may not match whatever a pretty printer "thinks" it should be.

The only real problem left is that of code-generated-by-code (any language, not necessarily PHP). It takes real discipline (and a little practice) to write the generating code in such a way that the resulting code (HTML or whatever) is readable and has its own proper indenting.

But "HTML embedded into PHP" is a real problem if you don't write it yourself such that the result will be "correct": there are just too many ways that PHP syntax allows you to generate code (and what it generates can be any code, not just HTML, but also any flavor of XML, PS, RTF, you name it), and a pretty printer cannot "see" what the result should be when it cannot interpret when what would be produced where: that is in the logic of the generating code. It would take a PHP interpreter, not just reading a single file but also includes, even conditional includes, which also detects what language is being generated and knows its syntax and the rules to be applied, to be able to automate how to indent the resulting code.

That is so hard, and, frankly, so useless, no one will likely even try to tackle it. Just write your code so that it produces well-laid-out code.

As an exercise, try this little problem:
Given an array of three elements in PHP, write out all the different ways you can produce a simple unordered list in HTML from that. The <ul> should have a class, and each of the <li> elements should also have a (single) class. How many can you find?

   
__________________
Marjolein Katsma
Look through my eyes on Cultural Surfaces (soon!), My ArtFlakes shop and Flickr.
Occasionally I am also connecting online dots... and sometimes you can follow me on Marjolein's Travel Blog
iamback is offline   Reply With Quote
Old 02-19-2008, 03:58 AM   #3
CarlSeiler
Member
 
CarlSeiler's Avatar
 
Join Date: Oct 2005
Location: Denton, TX
Posts: 271
Default

Quote:
Originally Posted by iamback View Post
I think that "pretty printers" may have gone out of fashion, actually.

The idea of course is to create "pretty" code off the bat, and decent text editors will help you these days with auto-indent features (even language-savvy auto-indent), so there's hardly any excuse left (for a human) to not write code that's well-indented.
I use Notepad++ for editing. It does have an auto-indent feature, which is--I think--pretty rudimentary. It's certainly not as intelligent as the auto indenting that Visual Basic Express does, for example. Of course, I have my own problems with VB Express.

As I mentioned in my original post, I try to create it as I go, but frequently, I lose my place in particularly long or complicated sections of php or (X)HTML.

Quote:
The other cause may be that there are many possible "rules" for indenting code (and software projects may impose theirs), which may not match whatever a pretty printer "thinks" it should be.
You're absolutely right about that. Sometimes I can't remember my own "rules," and when I get to the bottom, I realize things are slightly off, and going back can be a bit of a challenge.
CarlSeiler is offline   Reply With Quote
Old 02-23-2008, 07:43 AM   #4
dthomsen8
Member
 
dthomsen8's Avatar
 
Join Date: Aug 2005
Location: Philadelphia, PA 19130
Posts: 2,158
Default

Quote:
Originally Posted by CarlSeiler View Post
...
As I mentioned in my original post, I try to create it as I go, but frequently, I lose my place in particularly long or complicated sections of php or (X)HTML.

... Sometimes I can't remember my own "rules," and when I get to the bottom, I realize things are slightly off, and going back can be a bit of a challenge.
All that is about code we are writing now. The real difficulties lie in code someone else wrote, or code we ourselves wrote years ago before we knew how to make it legible and understandable later.

I suppose it is possible to do an include in the HTML for the PHP, and keep the PHP in separate files, but I would wonder if that is anything of a solution, since it might well be slower, and with more files, harder to read or change.
dthomsen8 is offline   Reply With Quote
Old 02-23-2008, 08:34 AM   #5
iamback
Member
 
iamback's Avatar
 
Join Date: Oct 2005
Location: Amsterdam, NL
Posts: 4,894
Default

Quote:
Originally Posted by dthomsen8 View Post
All that is about code we are writing now. The real difficulties lie in code someone else wrote, or code we ourselves wrote years ago before we knew how to make it legible and understandable later.
You're right about someone else's code. That's why software projects tend to have coding guidelines, though. As to your own code, written years ago, going through it manually to make a decent code layout will also help you familiarize yourself with what the heck it was supposed to be doing since if you didn't pay attention to code layout it's highly unlikely you were properly documenting your code either.

Quote:
I suppose it is possible to do an include in the HTML for the PHP, and keep the PHP in separate files
Unless what you do is extremely simple, it's usually the other way round: PHP scripts being the primary files, some of which generate HTML. The standard solution for that is to use templates to separate your business logic (PHP) from your presentation layer (HTML+CSS and optionally JavaScript). Each in their own files.

Quote:
I would wonder if that is anything of a solution, since it might well be slower, and with more files, harder to read or change.
No, if you keep your code properly organized it is the only solution. Logic and presentation should be separate.

   
__________________
Marjolein Katsma
Look through my eyes on Cultural Surfaces (soon!), My ArtFlakes shop and Flickr.
Occasionally I am also connecting online dots... and sometimes you can follow me on Marjolein's Travel Blog
iamback is offline   Reply With Quote
Old 02-23-2008, 11:01 AM   #6
ktinkel
Founding Sysop
 
ktinkel's Avatar
 
Join Date: Oct 2004
Location: In Connecticut, on the Housatonic River near its mouth at Long Island Sound.
Posts: 11,189
Default

I wanted to repost a web page on my late but still fascinating in-laws. I had taken it down a couple of years ago when I was moving my site, and hadn’t looked at its code since I posted the page in 2002. Oy. What a mess.

I think it was done in the midst of my learning-to-use-tables phase. (I know — slow learner.) No DOCTYPE. No encoding info. No meta statements. And all those descriptive table statements. Oy, oy, oy.

After scratching my head for half an hour, I opened the HTML in BBEdit, previewed the page, and copied the text from the preview, and put it in a new HTML page. Then started writing the CSS. It feels like a breath of fresh air.

But I sure would hate to have to do this for a complicated site.

Just a comment; no advice for anyone else. Empathy, though.

   
__________________
[SIZE=2][COLOR=LemonChiffon]::[/COLOR][/SIZE]
[SIGPIC][/SIGPIC]
ktinkel is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML is Dead. Long live HTML. Five that is... CarlSeiler Web Design 3 07-30-2007 11:04 PM
XLS to HTML dthomsen8 Web Site Building & Maintenance 3 08-07-2006 03:53 PM
If you like HTML even a little... iamback Web Design 0 06-09-2006 12:15 AM
Semantic HTML? ktinkel Web Design 7 08-30-2005 11:50 AM
Help, Pretty Please? Franca Images 13 04-10-2005 03:34 PM


All times are GMT -8. The time now is 06:01 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Contents copyright 2004–2014 Desktop Publishing Forum and its members.