SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Jack Matier's Avatar
    Join Date
    Feb 2005
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text formatting & Indenting output. Best solution.

    I know the indent and new line characters. I know how to force my code to look a certain way by adding and removing \t within functions and strings themselves.

    What I would like to know is what's the best way for outputting strings into the final page so that when you "view source" it's nice and pretty. I don't have control of where the code in the page so I can't assume how many tab characters is needed.

    Let's take a look at what I'm talking about.
    PHP Code:
    <?php
    $string 
    = <<<EOD
    this text
    is not indented 
    once
    EOD;
    ?>

    <?=$string?>

        <?=$string?>
    Would output to:
    Code:
    this text
    is not indented 
    once
    
    	this text
    is not indented 
    once
    While I'd like it to be:
    Code:
    this text
    is not indented 
    once
    
    	this text
    	is not indented 
    	once
    I can, and did, create a function to help with formatting.
    PHP Code:
    <?php
    function t($str$l) {
        
    $t implode("\t",array_pad(array(), $l++, ''));
        return 
    str_replace("\n","\n".$t$str);
    }
    ?>
    and by using that function
    PHP Code:
        <?=t($string,1)?>
    I can have it output the desired amount of tabs in the view.
    Code:
    	this text
    	is not indented 
    	once
    Ideally though, a person wouldn't have to use that function to allow the multi-line string to have the correct indentation.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you don't control where in the page the code can be used, then you can't know in advance how many tabs to print.

  3. #3
    SitePoint Addict Jack Matier's Avatar
    Join Date
    Feb 2005
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't there a way to know the position in a document a function is so that one could call t('string') and have it use that position to do indenting? Perhaps abstracting the method a bit more.

    I know when debugging I use __LINE__ and __FILE__ sometimes.

    Or. How do you deal with this problem?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jack Matier View Post
    Or. How do you deal with this problem?
    This isn't a problem, so nobody actually deals with it. View the source of this page. It's got different intending levels all over the place.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The html output isn't the source code, it's just for the browser. You should concentrate on making your actual source files readable instead

  6. #6
    SitePoint Addict Jack Matier's Avatar
    Join Date
    Feb 2005
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    The html output isn't the source code, it's just for the browser. You should concentrate on making your actual source files readable instead
    Oh I most certainly don't ignore that... but I happen to think both the script and the markup should be formatted in such a way that can be understood without having to run it through a program.

    It sucks making a nicely marked up website and trying to keep it that way as it becomes dynamic.

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can run it through tidy if you really need to.

  8. #8
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this sort of topic is quite advance for me and really I picked up usefull info, thanks.


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
  •