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 \ 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
$string = <<<EOD
this text
is not indented 
once
EOD;
?>

<?=$string?>

	<?=$string?>

Would output to:

this text
is not indented 
once

	this text
is not indented 
once

While I’d like it to be:

this text
is not indented 
once

	this text
	is not indented 
	once

I can, and did, create a function to help with formatting.

<?php
function t($str, $l) {
	$t = implode("\	",array_pad(array(), $l++, ''));
	return str_replace("\
","\
".$t, $str);
}
?>

and by using that function


	<?=t($string,1)?>

I can have it output the desired amount of tabs in the view.

	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.

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.

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?

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.

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 :slight_smile:

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.

You can run it through tidy if you really need to.

this sort of topic is quite advance for me and really I picked up usefull info, thanks.