SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Intending the outputted HTML

    How do you do it?

    I just cannot figure out how to write PHP code so that the outputted HTLM would be nicely indented.

    This is my usual way of writing PHP in template files (I have created a dummy posts table with some random data):

    PHP Code:
    <div>
        <?php foreach ($posts as $post): ?>
        <h2><?php echo $post->title?></h2>
        <?php echo $post->content?>
        <?php endforeach; ?>
    </div>
    Which outputs:

    Code:
    <div>
            <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
    </div>
    I would like something nicely indented like this:

    Code:
    <div>
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
    </div>

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    Code:
    <div>
    
        <?php foreach ($posts as $post): ?>
    
        <h2><?php echo $post->title; ?></h2>
    
        <?php echo $post->content; ?>
    
        <?php endforeach; ?>
    
    </div>

    the tab before the foreach is whats causing the extra index
    PHP Code:
    <div>
    <?php foreach ($posts as $post): ?>
        <h2><?php echo $post->title?></h2>
        <?php echo $post->content?>
    <?php 
    endforeach; ?>
    </div>

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You needn't care about whether it looks nice when outputted. What matters is if it looks nice when you read it in your template files.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daniel0 View Post
    You needn't care about whether it looks nice when outputted. What matters is if it looks nice when you read it in your template files.
    I used to think that before I started developing large web applications. Trying to spot a bug when the source code is neatly indented is a godsend, trust me.

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    the tab before the foreach is whats causing the extra index
    PHP Code:
    <div>
    <?php foreach ($posts as $post): ?>
        <h2><?php echo $post->title?></h2>
        <?php echo $post->content?>
    <?php 
    endforeach; ?>
    </div>
    I know but then it looks bad in the template file.

  6. #6
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I have made this:

    PHP Code:
    <div>

        <h1>Test</h1>
        
    <?php foreach ($posts as $post): ?>

        <h2><?php echo $post->title?></h2>
        <?php echo $post->content?>

    <?php endforeach; ?>

    </div>
    Which outputs:

    Code:
    <div>
        
        <h1>Test</h1>
        
    
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
    
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
    
        <h2>Test Post</h2>
        <p>Some content here...</p><p>Some content here...</p><p>Some content here...</p>
        
    </div>
    Template file now looks bit ugly but I guess there's no other way. Compromise.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SJH View Post
    I used to think that before I started developing large web applications. Trying to spot a bug when the source code is neatly indented is a godsend, trust me.
    You can use a tool like tidy to tidy it up. Example:
    Code:
    degeberg ~ # echo "<div><h1>Test</h1><ul><li>item 1</li><li>item 2</li></ul></div>" > test.html
    degeberg ~ # tidy -iq test.html
    line 1 column 1 - Warning: missing <!DOCTYPE> declaration
    line 1 column 1 - Warning: inserting missing 'title' element
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
    
    <html>
    <head>
      <meta name="generator" content=
      "HTML Tidy for Linux (vers 1 September 2005), see www.w3.org">
    
      <title></title>
    </head>
    
    <body>
      <div>
        <h1>Test</h1>
    
        <ul>
          <li>item 1</li>
    
          <li>item 2</li>
        </ul>
      </div>
    </body>
    </html>
    You could also run something like mod_tidy on your development server.

  8. #8
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    I know but then it looks bad in the template file.
    And by the way that didn't work either. Seems like my editor needs and extra empty line in code to indent properly.

    I'm using Komodo Edit 5.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •