Salida válida y limpia (x) HTML con TidyHelper
Pequeña ayuda que puede utilizar a la salida válida, minified y limpia (x) HTML de forma automática!
Requisitos
1. Ordenada habilitado en su servidor: http://php.net/manual/en/book.tidy.phpCómo utilizar
En primer lugar, agregar la ayuda de tu AppController con sus otros ayudantes:Controlador de la clase:
<?php
class AppController extends Controller {
var $helpers = array('Html', 'Js', 'Session', 'Tidy');
}
?>
El TidyHelper
Luego de crear el archivo TidyHelper en app / views / helpers / tidy.php:Ayudante de clase:
<?php
/**
* TidyHelper to clean HTML output
*
* @author Thiago Belem <contato@thiagobelem.net>
*/
class TidyHelper extends AppHelper {
/**
* TidyHelper constructor
*
* @see TidyHelper::filter()
*
* @uses Configure::read()
*
* @return void
*/
public function __construct() {
// If the function exists and is production mode
if (function_exists('tidy_repair_string') && !Configure::read('debug'))
ob_start(array(__CLASS__, 'filter'));
}
/**
* Filter the output with TidyHTML
*
* @uses {@link http://php.net/manual/en/tidy.repairstring.php tidy_repair_string()}
* @uses Configure::read()
*
* @param string $output The HTML output
*
* @return string
*/
static function filter($output) {
// Convert "UTF-8" to "utf8"
$encoding = low(str_replace('-', '', Configure::read('App.encoding')));
// Clean the output using Tidy
$output = tidy_repair_string($output, array(
'indent' => false, // Remove indentation
'hide-comments' => true, // Remove the comments
'drop-proprietary-attributes' => true, // Remove Office attributes from HTML tags
'wrap' => false, // Don't break each line after 80 chars
'output-xhtml' => true, // Output as xHTML
'char-encoding' => $encoding // The input/output encoding
), $encoding);
return $output;
}
}
?>
