Search code examples
htmlnewlinedoctypespaces

how to minimize the effect of space or newline in HTML?


First of all, my question may be unclear. I will try to explain it.

this is my html

<div class="left"><?php print $search_box; ?><?php if (!empty($logo)): ?><a href="<?php print $base_path; ?>" title="<?php print t('Home'); ?>" rel="home"><img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" id="logo" width="243" height="62" /></a><?php endif; ?><?php if (!empty($site_name)): ?><div id='site-name'><strong><a href="<?php print $base_path; ?>" title="<?php print t('Home'); ?>" rel="home"><?php print $site_name; ?></a></strong></div><?php endif; ?><?php if (!empty($site_slogan)): ?><div id='site-slogan'><?php print $site_slogan; ?></div><?php endif; ?></div>

looks ugly and difficult to debug, right?

so i try to indent and add newline. However, it will fails on some browser, may be IE6. The result is changed. So, What should i go, should i use another doctype?

Currently, i am using

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Solution

  • You can write it this way too:

    <div class="left">
      <?php print $search_box; ?>
        <?php if (!empty($logo)) { ?>
        <a href="<?php print $base_path; ?>" title="<?php print t('Home'); ?>" rel="home">
          <img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" id="logo" width="243" height="62" />
        </a>
      <?php } ?>
    
      <?php if (!empty($site_name)) { ?>
        <div id='site-name'>
          <strong>
          <a href="<?php print $base_path; ?>" title="<?php print t('Home'); ?>" rel="home"><?php print $site_name; ?></a>
          </strong>
        </div>
      <?php } ?>
    
      <?php if (!empty($site_slogan)) { ?>
        <div id='site-slogan'><?php print $site_slogan; ?></div>
      <?php } ?>
    </div>
    

    This should work in most cases, or you can use the php heredoc syntax to echo out the html stuff normally.