Page 1 of 1

Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 10:27 am
by mygind
Dear all,

I was wondering if anyone could point me to a place going through the subject making both php-source and the html-output (page source).

Lets say I have a these functions:
printCell($contents){
return "<td>" . $contents . "<td>";
}

printLink($contents){
return "<a>" . $contents . "</a>";
}

Performing this action
echo printCell(printLink("Link"));

Results in an output that looks
<td><a>Link</a><td>

I would like it to print something loke this
<td>
[tab]<a>Link</a>
</td>

This would also be ok
<td>
[tab]<a>
[tab][tab]Link
[tab]</a>
</td>

I wonder if this is possible in some easy way?

I thought about using a $indent variable - but somehow it feels awkward to write:
printMainTable($indent){
return
$indent . "<table>" . "\n" .
$indent . "\t <tr>" . "\n" .
$indent . "\t\t <td>Title</td>" . "\n" .
$indent . "\t </tr>" . "\n" .
$indent . "</table>";
}
And the source suddenly turns into code which I would like to prevent.

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 10:36 am
by jayshields
If I understand you correctly you could just use CSS to style your elements. Something like the margin-left attribute should do the trick, but I don't know how these source formatters work though, so maybe just look into their CSS.

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 10:59 am
by mygind
The css would change how the text is formatted on output from html to what you see (html->see), right?

What I want is my text to look pretty when you read page source (in firefox rightclick and press view page source) that is formatting the output from php to html (php->html)

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 11:06 am
by jayshields
What's the point in that? On topic, you can use \t and \n for tabs and new lines, respectively.

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 11:42 am
by mygind
It makes it easier to find whatever you might look for in the html source - say a link hidden behind a picture etc.
And then it looks as if it was written by hand as in good old times ;)

To me it seems right at least...

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 12:06 pm
by jayshields
Well yeah, try using "\t" - in double quotes.

...but I still don't know why you'd go out of your way to make your HTML source look nice. I agree that by principle you should try to make it look nice, but when it requires extra work, just leave it - it doesn't really matter as long as your comfortable with the formatting in the development view. If anyone really wants to inspect your source they can just paste it into their favourite editor.

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 12:30 pm
by php_east
readablity perhaps, i do sometimes wish i do the same for my html output when problems arises.

anyways, try this function, maybe it will suit you, as you need not worry about formating when you are forming the html proper.

Code: Select all

$a='<td><a>Link</a><td>';
print_r( implode('>'.chr(13),explode('>',$a)) );
 

Re: Pretty source AND html-output (page source) ?

Posted: Mon Mar 23, 2009 12:37 pm
by JAB Creations
If you want line breaks to make your (X)HTML output humanly readable then use \n.

In example...

Code: Select all

<?php
echo '<div><p>some text here</p></div>'."\n";
?>