470,870 Members | 1,411 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

indenting

Dear readers,

If you use some funtion to generate html code like:

<?
function display_stuff( parameters)
{
echo "<p>\n";
echo " <table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"
width=\"100%\">\n";
/* .. more stuff here... */
echo " </table>\n";
echo "</p>\n";
}
?>

Is there some way to know your 'current horizontal position' so you can
indent a html section properly?

Tia, Jean
Jan 4 '06 #1
5 1698
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jean Pion wrote:
Is there some way to know your 'current horizontal position' so you can
indent a html section properly?


Ugh.

You may want to use Output Buffering along with the Tidy functions.
http://php.net/manual/en/ref.outcontrol.php
http://php.net/manual/en/ref.tidy.php

(And - indenting HTML code is not as important as making clean,
standards-compliant, valid HTML code).

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDu/bY3jcQ2mg3Pc8RAn+dAJ9nKfBduWiHByebT4SBBh+o6vQgBQCf c5cD
8j+DvfHPubYwhvUbgJAdjJc=
=9zBL
-----END PGP SIGNATURE-----
Jan 4 '06 #2
Jean Pion wrote:
Dear readers,

If you use some funtion to generate html code like:

<?
function display_stuff( parameters)
{
echo "<p>\n";
echo " <table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"
width=\"100%\">\n";
/* .. more stuff here... */
echo " </table>\n";
echo "</p>\n";
}
?>

Is there some way to know your 'current horizontal position' so you can
indent a html section properly?

Tia, Jean

As Iván alludes to, the great thing about php is you're really writing a
program, not html. So, in the php commands you'll want neat code. That
will benefit you when you go back to it. I would recommend also
including comments about what is going on, so you don't have to try to
remember 6 months from now. But the only reason to generate neat html
is so the users can look at the page source and make sense of it more.

Tom
Jan 4 '06 #3
Jean Pion wrote:
Is there some way to know your 'current horizontal position' so you can
indent a html section properly?


When I care about indentation (which is not very often), I tend to achieve
it by passing an $indent parameter to the function, which takes a string
of whitespace and prepends it to each line.

Example follows...

================================================

<?php

function createDiv ($innerText, $indent='')
{
$innerText = str_replace("\n", "\n{$indent} ", $innerText);
return "{$indent}<div>\n"
. "{$indent} {$innerText}\n"
. "{$indent}</div>";
}

$divInner = createDiv("Hello world");
$divMiddle = createDiv($divInner);
$divOuter = createDiv($divMiddle, ' ');

?>
<html>
<body>
<?= $divOuter ?>

</body>
</html>

================================================

Which should generate:

<html>
<body>
<div>
<div>
<div>
Hello
</div>
</div>
</div>
</body>
</html>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '06 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tomb wrote:
But the only reason to generate neat html
is so the users can look at the page source and make sense of it more.


Besides, a developer should not read raw HTML, but should have a look at the
DOM tree - there are wonderful DOM tree inspectors for Konqueror and
Firefox (built-in) and for IE (via third-party toolbars).

Analyzing the DOM tree generated by the PHP scripts, and validating the
generated HTML against W3C's validators would prove more useful than
writing scripts that generate indented HTML.

(In fact, my programs generate a gibberish of (X)HTML that validates quite
nicely, but is a mess to read).

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
5.0.5-3 generating this signature.
Uptime: 02:03:19 up 1 day, 4:28, 1 user, load average: 0.43, 0.32, 0.14

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDvHD63jcQ2mg3Pc8RAtDlAJsH3Lqab9NYhQC3Dift98 up+Fv9UQCfalu1
PUVFbiUKEaDQYATcGPpmM7M=
=vvGt
-----END PGP SIGNATURE-----
Jan 5 '06 #5

"Iván Sánchez Ortega" <i.***************@rroba--mirame.punto.net> schreef in
bericht news:m1************@blackspark.escomposlinux.org.. .
tomb wrote:
But the only reason to generate neat html
is so the users can look at the page source and make sense of it more.
Besides, a developer should not read raw HTML, but should have a look at
the
DOM tree - there are wonderful DOM tree inspectors for Konqueror and
Firefox (built-in) and for IE (via third-party toolbars).

Analyzing the DOM tree generated by the PHP scripts, and validating the
generated HTML against W3C's validators would prove more useful than
writing scripts that generate indented HTML.


I'm just working on some inherited code, and I don't want to rewrite it
completely.
What I do now is load source of the resulting page into CSE Html Validator.
If would help me a bit to make it more readable;
but you are right: a comment blocks will do fine.

Thanks for your input,
Jean.

(In fact, my programs generate a gibberish of (X)HTML that validates quite
nicely, but is a mess to read).

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Jan 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David Ehmer | last post: by
2 posts views Thread by RobG | last post: by
9 posts views Thread by bob | last post: by
1 post views Thread by sh4r4d | last post: by
2 posts views Thread by Leon Friesema | last post: by
7 posts views Thread by Harrie | last post: by
reply views Thread by D. Shane Fowlkes | last post: by
6 posts views Thread by eugene.morozov | last post: by
4 posts views Thread by Christopher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.