Jump to content

[SOLVED] Retaining source formatting during programming


Ninjakreborn

Recommended Posts

When you code in Xhtml/css it's easy to mantain the formatting and keep everything need for when you go to view source.  However when I put out a lot of dynamic xhtml here and there, it look's really messed up (as far as indentation on the view source).  Not to say that I don't use /n which help's a lot.  It put's the spacing in the source code, however I have my html formatted, with my php output fomatted left justified, everything work's out like this

 

<html>

  <head>

    <title>Title Here</title>

  </head>

  <body>

    <form>

 

 

    </form>

 

 

php out put here.

<ul>

whatever

</ul>

 

 

  </body>

 

</html>

 

It alway's turned out malformed, hard to read, and not good to lookat.  However that only happen's with my php output, is there a way to retain the indentation, and everything just as if it was straight xhtml?

Link to comment
Share on other sites

You can either:

 

a) Not care about the specific layout of your markup.

 

b) Implement a beautifier as a last-stage process (which will add a bit of overhead, but neglible)

 

c) Use a markup generator, such as the DOM model available in the PHP SPL.

Link to comment
Share on other sites

If you want to format the output add in spaces to indent the outputted code not just \n, example:

 

<html>
  <head>
<?php echo "    <title>My Title</title>\n  </head>\n"; ?>
  <body>

    <p>Select year:
<?php
  echo "      <select>\n";

  for($i = 2010; $i > 2000; $i--)
  {
    echo "        <option value=\"$i\">$i</option>\n";
  }

  echo "      </select>\n";

?>
    </p>

  </body>
</html>

 

output - lovely formatted code:

<html>
  <head>
    <title>My Title</title>
  </head>
  <body>

    <p>Select year:
      <select>
        <option value="2010">2010</option>
        <option value="2009">2009</option>
        <option value="2008">2008</option>
        <option value="2007">2007</option>
        <option value="2006">2006</option>
        <option value="2005">2005</option>
        <option value="2004">2004</option>
        <option value="2003">2003</option>
        <option value="2002">2002</option>
        <option value="2001">2001</option>
      </select>
    </p>

  </body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.