CodeIgniter Forums
Maintain (X)HTML Indentation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Maintain (X)HTML Indentation (/showthread.php?tid=15610)



Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]underskor[/eluser]
How do you stop PHP reverting the indent level? For example:
Code:
<?php
$variable = '<p>Lorem ipsum sit dolor amet.</p>'.chr(10);
?&gt;
[..]
    &lt;body&gt;
        &lt;?php
        echo $variable;
        echo $variable;
        echo $variable;
        echo $variable;
        ?&gt;
    &lt;/body&gt;
produces the following HTML:
Code:
[..]
    &lt;body&gt;
        <p>Lorem ipsum sit dolor amet.</p>
<p>Lorem ipsum sit dolor amet.</p>
<p>Lorem ipsum sit dolor amet.</p>
<p>Lorem ipsum sit dolor amet.</p>
    &lt;/body&gt;
I'd like it to be:
Code:
[..]
    &lt;body&gt;
        <p>Lorem ipsum sit dolor amet.</p>
        <p>Lorem ipsum sit dolor amet.</p>
        <p>Lorem ipsum sit dolor amet.</p>
        <p>Lorem ipsum sit dolor amet.</p>
    &lt;/body&gt;

Hoping there's a way to do this which doesn't involve mutilating my nicely indented/formatted PHP.


Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]thinkigniter[/eluser]
try

Code:
&lt;body&gt;
        &lt;?php
        echo "\t\t".$variable;
        echo "\t\t".$variable;
        echo "\t\t".$variable;
        echo "\t\t".$variable;
        ?&gt;
    &lt;/body&gt;



Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]underskor[/eluser]
That works great, except it counts as "mutilating my nicely indented/formatted PHP". Big Grin I was hoping there would be some kind of solution which doesn't require me to add tab characters before each output, because I have to manually do so.

I don't understand why it does that. The first echo indents at the correct level, but the following echo's do not?


Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]TheFuzzy0ne[/eluser]
I would have thought something like this would work:

Code:
&lt;body&gt;
        &lt;?php echo $variable; ?&gt;

        &lt;?php echo $variable; ?&gt;

        &lt;?php echo $variable; ?&gt;

        &lt;?php echo $variable; ?&gt;
    &lt;/body&gt;

I think that the blank lines are necessary to stop the variables being appended to a single line.



Or, you could incorporate some kind of loop like this:

Code:
&lt;body&gt;
&lt;?php foreach ($something as $something_else): ?&gt;
        &lt;?php echo $something_else; ?&gt;

&lt;?php endforeach; ?&gt;
&lt;/body&gt;

Again, if memory serves correctly, you need the empty line to prevent the variables being appended to the same line.

It just takes a bit of trial and error, and a bit of practice, and then you will start to learn what needs to be done.

Hope this helps.


Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]TheFuzzy0ne[/eluser]
[quote author="underskor" date="1234272703"]I don't understand why it does that. The first echo indents at the correct level, but the following echo's do not?[/quote]

Simple. The PHP parser doesn't care about spaces and tabs, (as long as there's at least one where needed of course)

PHP simply outputs what's in the quotes, not what's outside them.

Code:
&lt;?php echo                "this line is not indented when printed out"; ?&gt;

&lt;?php echo "strangely enough, this line is indented to the same level as the string above"; ?&gt;

The only outputting PHP does, is what's between the quotes, not what's outside of them. Any padding needs to be added a) inside the string quotes, or b) outside of the &lt;?php ?&gt; tags.

Hope this helps.


Maintain (X)HTML Indentation - El Forum - 02-10-2009

[eluser]underskor[/eluser]
I see, thanks Fuzz. Did some testing and it seems that the blank line doesn't matter, it's just the seperate blocks of PHP.

eg.
Code:
&lt;?=$variable; ?&gt;
    &lt;?=$variable; ?&gt;
    &lt;?=$variable; ?&gt;
    &lt;?=$variable; ?&gt;

Thanks for your replies everyone.