Page 1 of 1

Perfectionist's code

Posted: Wed Dec 01, 2004 4:47 pm
by Todd_Z
Hello all...

Here's the problem: When i include() files, i dont have those included files formatted to be indented the certain amount that would fit the place that they are being put in, so basically im trying to find a way to include a file but put a leader on each line that would consist of a string of "\t"s. Thanks in advance...

Posted: Wed Dec 01, 2004 4:59 pm
by rehfeld
are you including php code or just text/html?

Posted: Wed Dec 01, 2004 5:03 pm
by Todd_Z
php code

Posted: Wed Dec 01, 2004 5:07 pm
by protokol
Umm, why does this even matter to you? Just make sure each file is formatted the correct way. Because when you include() it, it's not like it permanently links the contents of file B to file A. You are basically just referencing it.

Posted: Wed Dec 01, 2004 5:12 pm
by rehfeld

Code: Select all

<?php


ob_start();
include('script.php');
$generated_html = ob_get_contents();
ob_end_clean();

echo str_replace("\r\n", "\r\n\t\t\t", $generated_html);


?>
you may need to replace \r\n w/ just \n or \r depending on what your script.php outputs. but most likely \r\n will work

its kinda resource intensive if its just for cosmetic purposes though...

Posted: Wed Dec 01, 2004 5:13 pm
by Todd_Z
Mmmm title of subject - Perfectionist's code - I like to have purdy coding.... Second -> Obviously its just referencing, but i dont feel like making all those other files having like 14 indentations before the coding...

Posted: Wed Dec 01, 2004 5:25 pm
by josh
Todd_Z wrote:Obviously its just referencing, but i dont feel like making all those other files having like 14 indentations before the coding...
You don't get what he's saying I don't think... you will never actually see the 2 files together so it doesnt matter:

file.php

Code: Select all

<?php
include("file2.php");
?>


file2.php

Code: Select all

<?php
echo "hello";
?>

You will never ever see the word echo or the word hello on file.php unless you are useing some php editor that shows the code when you do an include.

Posted: Wed Dec 01, 2004 5:51 pm
by Todd_Z
Source code in a browser

Posted: Wed Dec 01, 2004 6:11 pm
by John Cartwright
Then you are referring to the HTML output, not the php code itself.

Posted: Wed Dec 01, 2004 6:30 pm
by Todd_Z
Right but since i already tried to find an output source formatter ( No, Tidy is not what i mean ) I hoped that i could do something along the lines of prefixing each line of the include with a string of \t's. Impossible to do?

Posted: Wed Dec 01, 2004 7:22 pm
by djot
-
How would you ensure that a special line is indended 3 tabs, and the other one e.g. 5 tabs? Sounds rather unlogic.

If this is so important to you to have a clean HTML structure, you should follow one coding stlye in all your projects.

djot
-

Posted: Wed Dec 01, 2004 7:25 pm
by timvw
guess which library has these options? :P

indent: no, yes or auto
If set to yes, *library* will indent block-level tags. The default is no. If set to auto *library* will decide whether or not to indent the content of tags such as title, h1-h6, li, td, th, or p depending on whether or not the content includes a block-level element. You are advised to avoid setting indent to yes as this can expose layout bugs in some browsers.
indent-spaces: number
Sets the number of spaces to indent content when indentation is enabled. The default is 2 spaces.
indent-attributes: bool
If set to yes, each attribute will begin on a new line. The default is no.

Posted: Wed Dec 01, 2004 7:31 pm
by rehfeld
Todd_Z wrote:Right but since i already tried to find an output source formatter ( No, Tidy is not what i mean ) I hoped that i could do something along the lines of prefixing each line of the include with a string of \t's. Impossible to do?
does my example not work?

Posted: Wed Dec 01, 2004 9:30 pm
by Todd_Z
Thanks rehfeld - it worked... much appreciation.

Code: Select all

<?php
function cleanInclude ($filename, $indentation) {
	ob_start();
	include($filename);
	$generated_html = ob_get_contents();
	ob_end_clean();
	echo str_replace("\n", "\n".$indentation, $generated_html);
}
?>
Okay, so there's the code...
Second problem - If i have an include inside of an include, this doesn't work (my guess because of the ob and it already being dealt with) Anyways, is there a solution?