\n"; for($i = 1; $i <= 10; $i++...">

How do you backtrack from your HTML?

Given the HTML generated by my application.

function pagination(){ echo "<ul>\n"; for($i = 1; $i <= 10; $i++) echo "\t<li>...</li>\n"; echo "</ul>\n"; } ?> <div> <?php pagination(); ?> </div> 

If I add another DIV container, this will not produce correctly printed code.

Is there any solution for the function to somehow find out how many \ t or spaces to add, or how the html automatically indent somehow?

+8
html php indentation
source share
10 answers

Amazing question.

There are 9 answers and 3 comments so far, and no one seems to have bothered to read the question, but simply repeated some of the gospel brought up by the keyword in the title - the most preferred way to answer questions about the blessed stackoverflow site.

But the question is not that simple / single layer.
I must admit that this is ambiguous. So we have to dig it.

1) How do you backtrack from HTML?

Use patterns, dude. Use patterns. The only answer.

2) Is there any solution for the function to somehow find out how many \ t or spaces to add, or does the html automatically back off somehow?

Of course not .
PHP knows nothing about HTML, indentation, etc.
Especially when HTML is not ready yet (!)

3) If I add another DIV container, this will not lead to the creation of correctly printed code.

Question a question.
The question for which the question was asked.

And yet they are the hardest of all.

And partly the answer I showed was a complete disagreement, hehe:
Although the relative order of the tags is important, for the resulting large HTML, you can move some blocks from the line:

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Hello</h1> <div> <!-- news list --> <div> <ul> <li>1..</li> <li>2..</li> <li>3..</li> <li>4..</li> </ul> </div> <!-- /news list --> </div> <div> <!-- pagination --> <ul> <li>Red</li> <li>Green</li> <li>Blue</li> <li>Black</li> </ul> <!-- /pagination --> </div> </body> </html> 

This will allow you to have the right margin in meaningful blocks, but still keep the basic HTML in order.
As a side effect, it will keep your lines on the screen :)

To maintain a good indentation within the subpatterns, I highly recommend using PHP-based templates. Not an ugly HEREDOK for God's sake!
Here is just one rule for PHP templates:
always leave PHP blocks on the left side. Everything. To keep indentation between nested PHP blocks, just paste them inside <? ?> <? ?>

Example:

 <ul> <? foreach ($thelist as $color): ?> <li> <? if ($color == $current): ?> <b><?=$color?></b> <? else ?> <a href="?color=<?=$color?>"><?=$color?></a> <? endif ?> </li> <? endforeach ?> </ul> 

This will lead to the correct indented HTML, while maintaining the order of both HTML and PHP in the template, which will simplify the life of the developer both in development and in debugging.

Do not listen to anyone who says, “You don’t need to embed your code at all!” They are just lovers, not real developers. Anyone who has an idea of ​​what debugging is, who has trouble debugging his code, would tell you that the right indentation is important.

+13
source share

The answer may seem strange, but you should not worry about the generated indentation of the code. Indentation is for readability and should only be worried on the side of the programmer, and not on the generated part (which is for browsers).

+3
source share

I agree with all the comments that do not bother, but if you have a case where it makes sense, you can send your HTML via HTML Tidy (or in your case PHP Tidy ) using the indent option.

+1
source share

If you add \ t in front of ul, you should see that the code is interrupted

 <?php function pagination(){ echo "\t<ul>\n"; for($i = 1; $i <= 10; $i++) echo "\t\t<li>...</li>\n"; echo "\t</ul>\n"; } ?> <div> <?php pagination(); ?> </div> 
0
source share

Most editors have some formatting function that can be used to correct indentation. In Visual Studio, for example, you can press Ctrl + K + D to nicely format your html.

0
source share

I usually do this something like this if this is a more complex html structure. This makes tracking easier if you ask me, and you can also write your HTML code, rather than worry about escaping quotes, etc.

Since you are not in the PHP block when you process HTML, it appears as indentation as you do it.

 <?php function pagination(){ echo "</ul>\n"; for($i = 1; $i <= 10; $i++) { ?> <li>...</li> <?php } echo "</ul>\n"; } ?> <div> <?php pagination(); ?> </div> 
0
source share

You can use heredoc syntax

 <?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; /* More complex example, with variables. */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 Notice all of the indenting is preserved. EOT; ?> 
0
source share

Answer: use templates .

If you correctly separate your logic from your presentation, this question will go away. Patterns will have all the indentation or lack of indentation that you want or need.

EDIT

However, you can quite easily change your code:

 <?php function pagination($depth=0) { $indent = str_pad("", $depth, "\t"); echo $indent."<ul>\n"; for($i = 1; $i <= 10; $i++) echo $indent."\t<li>...</li>\n"; echo $indent."</ul>\n"; } ?> <div> <?php pagination(1); ?> </div> <div> <div> <?php pagination(2); ?> </div> </div> 

Alternatively, you can simply pass the required indent:

 <?php function pagination($indent="") { echo $indent."<ul>\n"; for($i = 1; $i <= 10; $i++) echo $indent."\t<li>...</li>\n"; echo $indent."</ul>\n"; } ?> <div> <?php pagination("\t"); ?> </div> <div> <div> <?php pagination("\t\t"); ?> </div> </div> 
0
source share

I also support well-formed code. Many people said that "firebug will print your code for you." Well, what about when the markup delivered to the browser is modified or injected into the DOM via javascript? Then firebug shows you the current state, not what you downloaded from the server.

I also like when my html is visible and readable throughout my script. Usually I do something like this, tracking the indentation of PHP and HTML separately (no need to hover over the opening tag or { and just scroll the page until you find the closing tag or } conveniently positioned from the position of your cursors.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Hello</h1> <div><?php $thelist = array('Red', 'Green', 'Blue', 'Black'); echo ' <ul>'; foreach ($thelist as $color) { echo ' <li>' . $color . '</li>'; } echo ' </ul>'; ?> </div> </body> </html> 

It prints the same way

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Hello</h1> <div> <ul> <li>Red</li> <li>Green</li> <li>Blue</li> <li>Black</li> </ul> </div> </body> </html> 

The notification looks as it should, and as in the script (this makes debugging easy for me). I use single quotes with HTML, so I don’t need to avoid trillions of quotes (this makes several concatenating strings more manual, but it works less than all these backslashes, which also prevents readability in the script).

Once again, this is how I do things and cannot be the perfect way to handle HTML formatting. I just want my HTML to be nice and tidy even when mixed with PHP

0
source share

Edit: broke the code on the page.


This code really only works with the template system.
What could just be basic:

 // file: index.tpl.html <html> <head> </head> <body> <div>Some header code</div> <div> {mainContent} </div> <div>Some footer code</div> </body> </html> // file: functions.php <?php function pagination() { $output = "<ul>\n"; for($i = 1; $i <= 10; $i++) $output = "\t<li>...</li>\n"; $output = "</ul>\n"; return $output; } function indent_html($html, $tag, $new_html) { // magic indenting code: finds how many spaces are used on the line above it $spacePos = 0; $find = strpos($html, '{'.$tag.'}' ); while( $html[ $find-$spacePos-1] == ' ' ) $spacePos++; // Uses the indent from the line above to indent your new html return str_replace("\n", "\n".str_repeat(" ", $spacePos), $new_html); } ?> // file: index.php <?php $html = file_get_contents("index.tpl.html"); // magic indenting code: finds how many spaces are used on the line above it $spacePos = 0; $find = strpos($html, '{mainContent}' ); while( $html[ $find-$spacePos-1] == ' ' ) $spacePos++; // your pagination() needs to return html not output it $mainContent = pagination(); $mainContent = indent_html($html, $tag, $mainContent); // Uses the indent from the line above to indent your new html $mainContent = str_replace("\n", "\n".str_repeat(" ", $spacePos), $mainContent); // finally insert your html $html = str_replace("{mainContent}", $mainContent, $html); ?> 

Some modification may be required if you want to use tabs instead of spaces.
I use spaces as this is more of a cross browser / app.

0
source share

All Articles