CodeIgniter Forums
Human readable code with CodeIgniter? - 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: Human readable code with CodeIgniter? (/showthread.php?tid=40562)

Pages: 1 2


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]gunnarflax[/eluser]
Hi! I really dislike having site source code which isn't human readable and when I use CodeIgniter functions such as ul() it doesn't get aligned with the rest of the code indenting. Example (The first line's multiple underscores is because the forum otherwise stript the whitespace and didn't show the indenting as planned):

Code:
_________<div>
            &lt;?php
               $attributes = array('class'=>'custom_class', 'id'=>'custom_id');
               $list = array(
                  anchor('foo/bar', 'something1'),
                  anchor('lor/ips', 'something2'),
                  anchor('one/two', 'something3'));

               echo ul($list, $attributes);
            ?&gt;
         </div>

The output becomes (I've replaced the tag "a" with "anchor" because the forum didn't let me post links Tongue):
Code:
_________<div>
            <ul class="custom_class" id="custom_id">
   <li><anchor href="foo/bar">something1</a></li>
   <li><anchor href="lor/ips">something2</a></li>
   <li><anchor href="one/two">something3</a></li>
</ul>
         </div>
How can I get the output to have the correct indenting relative to the rest of the site source?

Thanks!


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]CroNiX[/eluser]
Does the browser care how pretty the code looks?

This isn't a CI problem, its a problem with dynamically generated code. There is no way for CI, or any scripting language that I know of, to check its own source code, figure out where the last element was placed and create a newly "properly" indented element.

So, it would have to be able to figure out the difference between the location of:
Code:
<ul>
and
    <ul>
Was that 1 tab or 4 spaces?

If this is important to you, the best you can probably do in your source code is output a tab, "\t" and maybe some newlines ("\n"), as many times as you would need just before using the helper to align it where you want it to go. Only you know where that might be. But then again, your browser doesn't give a rip what your source code looks like, only if is valid xhtml, and you are outputting extra code just to have your source code pretty? I'm sure there is a reason why you would want to spend time on this. My clients don't usually want to pay for this type of extra work (that affects nothing functionally and usually something only searchbots and the like "see").


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]gunnarflax[/eluser]
Ok, I guess you're right. I'm just not used to having source code which can't be read :/


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]CroNiX[/eluser]
Well you can always do something like:
Code:
echo "\t\t\t" . ul($list, $attributes) . "\n";
where you are using html helpers.


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]gunnarflax[/eluser]
[quote author="CroNiX" date="1302649824"]Well you can always do something like:
Code:
echo "\t\t\t" . ul($list, $attributes) . "\n";
where you are using html helpers.[/quote]

Nah, that just indents the first ul-tag, the others are still placed relative to the beginning of the line...


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]cmgmyr[/eluser]
You could always rewrite the _list() function to add in the extra tabs. Just create a new helper file and copy, paste, then change the current _list() function within your new file. Doing this will make sure you don't modify the core file.


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]WanWizard[/eluser]
If you want to waste CPU cycles by formatting code only a browser ever sees, you could use a HTML beautifier like tidy. There's even a mod_tidy so you can have Apache do the job transparently.


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]gunnarflax[/eluser]
If you guys don't use indenting when working with html and php then how do you keep track of which tags should be closed etc.? Or do you format the html and leave the php unformatted? Now I'm of course talking about the development environment and not the actual site source.

I edited the html, url and form helper to accept an indenting parameter just for the heck of it. It only utilizes a str_repeat() function so it cannot be that hard on the CPU. If anyone want to try it out you're welcome!

I guess that why I'm bringing this whole thing up is that I'm not used to working with machine generated code like you do with CodeIgniter.


Human readable code with CodeIgniter? - El Forum - 04-12-2011

[eluser]cahva[/eluser]
In the views the code can be intended and beautiful as you want. These are 2 different things. And BTW, if you want to see tidied up generated html, just use Firebug or other similar dev tool if you use other browsers.


Human readable code with CodeIgniter? - El Forum - 04-13-2011

[eluser]n0xie[/eluser]
[quote author="gunnarflax" date="1302666196"]If you guys don't use indenting when working with html and php then how do you keep track of which tags should be closed etc.?[/quote]
My IDE does that for me. So should yours.