Welcome Guest, Not a member yet? Register   Sign In
HTML Output, Clean formatting
#1

[eluser]Ian Jones[/eluser]
When dealing with multiple views it is difficult to control the cleanliness of the HTML output. When you view in a browser the source of a page output by a myriad of functions, is there a way to auto format / indent the HTML with CI / PHP?
#2

[eluser]John_Betong[/eluser]
[quote author="Ian Jones" date="1264003328"]When dealing with multiple views it is difficult to control the cleanliness of the HTML output. When you view in a browser the source of a page output by a myriad of functions, is there a way to auto format / indent the HTML with CI / PHP?[/quote]
 
Not exactly an answer to your question but I have just developed a way to toggle validation on http://johns-jokes.com and the linked programs give an option to display the formatted source code.

Try this link and it shows two links, one for HTML validation and the other for CSS validation.

http://johns-jokes.com/c_localhost

 
 
 
 
#3

[eluser]Ian Jones[/eluser]
Not sure I understand what your post was John?

I need way to catch the output before heading to the browser, and have it properly indented so it appears cleanly formatted.
#4

[eluser]Colin Williams[/eluser]
It's going to be very difficult to manage in your view files. You can guess how much indentation a view file will need, but if you load that view in different contexts, it's going to break indentation.

Why is this a requirement, anyway? If you did have a layer of output that just indented you code, that seems like useless processing to me (most of the time you want to optimize your output, not add useless bytes of indentation). Most modern HTML inspection programs (Webkit Inspector, Firebug, etc) will indent the code for you. And almost every IDE I've worked with has a feature for doing this.
#5

[eluser]John_Betong[/eluser]
[quote author="Ian Jones" date="1264005605"]Not sure I understand what your post was John?

I need way to catch the output before heading to the browser, and have it properly indented so it appears cleanly formatted.[/quote]

One solution to solving your formatted solution is to use FireFox.com and the following addon:

http://users.skynet.be/mgueury/mozilla/

It is a brilliant addon that after displaying your page shows either a red or green icon in the bottom of your browser indicating that the source code has or has no errors respectively.

Clicking on the icon popsup in the lower section of the page with the source code showing errors and warnings along with solutions to fix the problems.

Selecting the top "HTML Validator" link and the "Clean up the page" from the drop list box shows the code all nicely formatted.

http://www.graabr.com/l0ylRC/
 
 
 
#6

[eluser]John_Betong[/eluser]
[quote author="Ian Jones" date="1264005605"]Not sure I understand what your post was John?

I need way to catch the output before heading to the browser, and have it properly indented so it appears cleanly formatted.[/quote]
 
Here is some code (from another poster) to remove all the spaces and line-feeds in your code. You can modify the code to suit your requirements:
Code:
$result = $this->load->view(ýour_view', $data, TRUE);

  $_SESSION['zzz_after']  = ''; // strlen $result);
  $_SESSION['zzz_before'] = strlen($result);
    
  $search = array
            (
               '/\>[^\S ]+/s', // strip whitespaces after tags, except space
               '/[^\S ]+\</s', // strip whitespaces before tags, except space
               '/(\s)+/s'      // shorten multiple whitespace sequences
             );

  $replace = array
             (
               '>',
               '<',
               '\\1'
             );
  $result = preg_replace($search, $replace, $result);
  
  $_SESSION['zzz_after'] = strlen($result);
                                                                                
  $result .= '<p> Source code reduced by:'
          .       sprintf('%6.3f', $_SESSION['zzz_after'] / $_SESSION['zzz_before'])
          .'% </p>';

   echo $result;
&nbsp;
&nbsp;
&nbsp;
#7

[eluser]Ian Jones[/eluser]
Cheers guys. I'll think about it some more.

Colin you are right actually, what does it really matter. It would indeed add bytes.
#8

[eluser]andrewtheandroid[/eluser]
Hey Ian

If you are using firefox you can use this nifty extension "view source chart" - (edit: forgot to add this one is good especially when there are many nested divs it's also graphical too so it helps to visualise it)

https://addons.mozilla.org/en-US/firefox/addon/655

There's also another extension called "phoenix editor" that you can view the source then click "format" and it tidies and indents your HTML too.

https://addons.mozilla.org/en-US/firefox/addon/11708

As for doing it in the php code though I think you will end up giving yourself a headache because I've tried adding manual breaks once and I could never get it to work properly and I think it's a waste of time if someone wanted to view your code they would use an extension as above. Or like the others suggest you can use an IDE that has codeformat I think netbeans and eclipse should do it. Some even format the HTML code inside PHP code.
#9

[eluser]taewoo[/eluser]
use tidy
#10

[eluser]roj[/eluser]
http://php.net/manual/en/book.tidy.php

should do the trick as long as your server has it installed




Theme © iAndrew 2016 - Forum software by © MyBB