Outputting formatted html from php scripts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Thomson

    #1

    Outputting formatted html from php scripts

    Do people generally try and output nicely formatted html from their
    scripts?

    I have been trying to, with variable indenting for tables etc. It
    makes it easier to see the html and perhaps debug it, but it can make
    ugly looking scripts.

    What's the general consensus?

    Craig

  • CountScubula

    #2
    Re: Outputting formatted html from php scripts

    I do for the most part, but I have never had an ugly script becouse of it.

    I do not indent my tables too far in, some times I will restart the nesting
    at colom 0 for a table.

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools
    "Craig Thomson" <craig@spam.fre e> wrote in message
    news:76m030tf0m 0or1peancpecprs sepgp9ugh@4ax.c om...[color=blue]
    > Do people generally try and output nicely formatted html from their
    > scripts?
    >
    > I have been trying to, with variable indenting for tables etc. It
    > makes it easier to see the html and perhaps debug it, but it can make
    > ugly looking scripts.
    >
    > What's the general consensus?
    >
    > Craig
    >[/color]


    Comment

    • CountScubula

      #3
      Re: Outputting formatted html from php scripts

      sorry, I hit send too fast, here is the rest of my post.

      sometimes my tables outout like this:

      <table>
      <tr>
      <td>

      <table>
      <tr>
      <td>stuff</td>
      </tr>
      </table>

      <td>
      </tr>
      </table>

      --
      Mike Bradley
      http://www.gzentools.com -- free online php tools
      "Craig Thomson" <craig@spam.fre e> wrote in message
      news:76m030tf0m 0or1peancpecprs sepgp9ugh@4ax.c om...[color=blue]
      > Do people generally try and output nicely formatted html from their
      > scripts?
      >
      > I have been trying to, with variable indenting for tables etc. It
      > makes it easier to see the html and perhaps debug it, but it can make
      > ugly looking scripts.
      >
      > What's the general consensus?
      >
      > Craig
      >[/color]


      Comment

      • Pedro Graca

        #4
        Re: Outputting formatted html from php scripts

        Craig Thomson wrote:[color=blue]
        > Do people generally try and output nicely formatted html from their
        > scripts?
        >
        > I have been trying to, with variable indenting for tables etc. It
        > makes it easier to see the html and perhaps debug it, but it can make
        > ugly looking scripts.
        >
        > What's the general consensus?[/color]

        I don't :)

        I might put a few "\n" here and there (usually after </tr> or <br/>) but
        no indenting for the next line. If I want to look at the HTML produced I
        tidy it first with
        HTML Tidy ( @ http://www.w3.org/People/Raggett/tidy/ )
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • David Mackenzie

          #5
          Re: Outputting formatted html from php scripts

          On Mon, 16 Feb 2004 05:47:05 GMT, Craig Thomson <craig@spam.fre e>
          wrote:
          [color=blue]
          >Do people generally try and output nicely formatted html from their
          >scripts?
          >
          >I have been trying to, with variable indenting for tables etc. It
          >makes it easier to see the html and perhaps debug it, but it can make
          >ugly looking scripts.
          >
          >What's the general consensus?[/color]

          Personally, I don't bother.

          My PHP code is nicely indented, so debugging is easy from the server
          end. Also, I try and output fairly clean HTML (no nested tables, etc)
          which makes dubugging from the client end easier.

          I don't see the point in cluttering up one's script with \n's and
          \t's. The browser doesn't need them and it saves a few bytes bandwidth
          ;-)

          Here's a tip: Single quotes can be used in HTML for attributes, so you
          can write:

          print("<img src='whatever.p ng' alt='Whatever'> "); // nice.

          as opposed to:

          print("<img src=\"whatever. png\" alt=\"Whatever\ ">"); // ugly.

          --
          David ( @priz.co.uk )

          Comment

          • Alex Farran

            #6
            Re: Outputting formatted html from php scripts

            David Mackenzie writes:
            [color=blue]
            > Here's a tip: Single quotes can be used in HTML for attributes, so you
            > can write:[/color]
            [color=blue]
            > print("<img src='whatever.p ng' alt='Whatever'> "); // nice.[/color]
            [color=blue]
            > as opposed to:[/color]
            [color=blue]
            > print("<img src=\"whatever. png\" alt=\"Whatever\ ">"); // ugly.[/color]

            Good tip. Here's another one. If ?> is followed immediately by a
            newline then the newline will not be output. For example:

            <?php echo "cat"; ?>
            fish

            Will write this:

            catfish

            If you follow the end-of-line ?> with a space you get this:

            cat
            fish

            --

            __o Alex Farran
            _`\<,_ Analyst / Programmer
            (_)/ (_) www.alexfarran.com

            Comment

            • Pedro Graca

              #7
              Re: Outputting formatted html from php scripts

              David Mackenzie wrote:[color=blue]
              > Here's a tip: Single quotes can be used in HTML for attributes, so you
              > can write:
              >
              > print("<img src='whatever.p ng' alt='Whatever'> "); // nice.[/color]

              Why use double quotes for PHP at all?

              print('<img src="whatever.p ng" alt="whatever"> '); // nicer IMHO


              though, if I'm not using the return value of print() i prefer echo()
              using the parenthesis-less syntax

              echo '<img src="whatever.p ng" alt="whatever"> ';
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              • David Mackenzie

                #8
                Re: Outputting formatted html from php scripts

                On 16 Feb 2004 12:19:23 GMT, Pedro Graca <hexkid@hotpop. com> wrote:
                [color=blue]
                >David Mackenzie wrote:[color=green]
                >> Here's a tip: Single quotes can be used in HTML for attributes, so you
                >> can write:
                >>
                >> print("<img src='whatever.p ng' alt='Whatever'> "); // nice.[/color]
                >
                >Why use double quotes for PHP at all?[/color]

                I'm used to using double quotes for string literals from other
                languages.
                [color=blue]
                >print('<img src="whatever.p ng" alt="whatever"> '); // nicer IMHO[/color]

                They're both preferable to \"whatever\" .

                --
                David ( @priz.co.uk )

                Comment

                • Eric Bohlman

                  #9
                  Re: Outputting formatted html from php scripts

                  Pedro Graca <hexkid@hotpop. com> wrote in news:c0qcgb$19r noe$1@ID-
                  203069.news.uni-berlin.de:
                  [color=blue]
                  > David Mackenzie wrote:[color=green]
                  >> Here's a tip: Single quotes can be used in HTML for attributes, so you
                  >> can write:
                  >>
                  >> print("<img src='whatever.p ng' alt='Whatever'> "); // nice.[/color]
                  >
                  > Why use double quotes for PHP at all?
                  >
                  > print('<img src="whatever.p ng" alt="whatever"> '); // nicer IMHO[/color]

                  But that won't work if you need to interpolate the values of variables into
                  the generated output.

                  Comment

                  • Pedro Graca

                    #10
                    Re: Outputting formatted html from php scripts

                    Eric Bohlman wrote:[color=blue]
                    > Pedro Graca <hexkid@hotpop. com> wrote in news:c0qcgb$19r noe$1@ID-
                    > 203069.news.uni-berlin.de:
                    >[color=green]
                    >> David Mackenzie wrote:[color=darkred]
                    >>> Here's a tip: Single quotes can be used in HTML for attributes, so you
                    >>> can write:
                    >>>
                    >>> print("<img src='whatever.p ng' alt='Whatever'> "); // nice.[/color]
                    >>
                    >> Why use double quotes for PHP at all?
                    >>
                    >> print('<img src="whatever.p ng" alt="whatever"> '); // nicer IMHO[/color]
                    >
                    > But that won't work if you need to interpolate the values of variables into
                    > the generated output.[/color]

                    No, it will not. But I prefer to not interpolate

                    <?php
                    $oldname = 'Pedro Graca';
                    echo '<input type="text" name="username" value="', $oldname, '"/>';
                    ?>

                    is how I do it.


                    I agree that

                    <?php
                    $oldname = 'Pedro Graca';
                    echo "<input type='name' name='username' value='$oldname '/>";
                    ?>

                    looks more like HTML than my preferred version, but I find it more
                    difficult to know what is being printed when interpolation is used.
                    I don't care for HTML looking scripts :)

                    One of the first things I do to scripts of other people is
                    "deinterpol ate" it :)
                    --
                    --= my mail box only accepts =--
                    --= Content-Type: text/plain =--
                    --= Size below 10001 bytes =--

                    Comment

                    • Henk Verhoeven

                      #11
                      Re: Outputting formatted html from php scripts

                      I use a composition of objects to produce the output, usually in
                      combination with files that are included by the objects.

                      The included files contain most of the lay out and are mostly in html
                      mode. I usually keep the html in each file properly formatted to make it
                      readable. I do not bother about the formatting of the final output that
                      is produced by the objects together. I don't want to becuase that would
                      make it harder to reuse the objects in whatever combination i like.

                      If i need to see the overall object composition structure in the output,
                      i switch on the object's debugging mode so that the start and end of the
                      output of each composing object is announced in html comments.

                      Greetings,

                      Henk Verhoeven.

                      Craig Thomson wrote:
                      [color=blue]
                      > Do people generally try and output nicely formatted html from their
                      > scripts?
                      >
                      > I have been trying to, with variable indenting for tables etc. It
                      > makes it easier to see the html and perhaps debug it, but it can make
                      > ugly looking scripts.
                      >
                      > What's the general consensus?
                      >
                      > Craig
                      >[/color]

                      Comment

                      • Chung Leong

                        #12
                        Re: Outputting formatted html from php scripts

                        For a while I try to use heredoc, but soon gave up. Nowaday I do a lot of
                        <?=$var?> and <? foreach():?>... <? endforeach; ?>. Makes fixing HTML layout
                        issues a little easier than doing echos.

                        My HTML forms tend to look like this:

                        <input name="hello" value="<? echo htmlspecialchar s($hello); ?>">

                        <? $checked = araay($which => "checked"); ?>
                        <input name="which" type="radio" value="a" <?=$checked['a']?>>
                        <input name="which" type="radio" value="b" <?=$checked['b']?>>
                        <input name="which" type="radio" value="c" <?=$checked['c']?>>

                        <? $selected = array($cow => "selected") ; ?>
                        <select name="cow">
                        <option value="3" <?$selected[3]?>>Mad</option>
                        <option value="4" <?$selected[4]?>>Dead</option>
                        <option value="5" <?$selected[5]?>>Tastes like chicken</option>
                        </select>

                        Uzytkownik "Craig Thomson" <craig@spam.fre e> napisal w wiadomosci
                        news:76m030tf0m 0or1peancpecprs sepgp9ugh@4ax.c om...[color=blue]
                        > Do people generally try and output nicely formatted html from their
                        > scripts?
                        >
                        > I have been trying to, with variable indenting for tables etc. It
                        > makes it easier to see the html and perhaps debug it, but it can make
                        > ugly looking scripts.
                        >
                        > What's the general consensus?
                        >
                        > Craig
                        >[/color]


                        Comment

                        • Marcin Dobrucki

                          #13
                          Re: Outputting formatted html from php scripts

                          Eric Bohlman wrote:[color=blue]
                          > Pedro Graca <hexkid@hotpop. com> wrote in news:c0qcgb$19r noe$1@ID-
                          > 203069.news.uni-berlin.de:
                          >
                          >[color=green]
                          >>David Mackenzie wrote:
                          >>[color=darkred]
                          >>>Here's a tip: Single quotes can be used in HTML for attributes, so you
                          >>>can write:
                          >>>
                          >>>print("<im g src='whatever.p ng' alt='Whatever'> "); // nice.[/color]
                          >>
                          >>Why use double quotes for PHP at all?
                          >>
                          >>print('<img src="whatever.p ng" alt="whatever"> '); // nicer IMHO[/color]
                          >
                          >
                          > But that won't work if you need to interpolate the values of variables into
                          > the generated output.[/color]

                          print "<img src=\"whatever. jpg\" alt=\"$descript ion\">\n";

                          no problemo.

                          /m

                          Comment

                          Working...