我正在使用 CI 来开发网站。一般来说,我们编写 HTML 代码如下:
<header>
<div class="banner">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>Site Title</h1>
<h2>Sub Totle</h2>
<div class="logo"></div>
</div>
</div>
</div>
</div>
</header>
之后我们使用 CI 加载 View ,如下所示:
$this->load->view('view');
现在我的问题是,如何通过替换views/view.php文件中的所有选项卡和新行来加载此 View ,如下所示?
<header><div class="banner"><div class="container"><div class="row"><div class="col-xs-12 ac"><h1>Site Title</h1><h2>Sub Totle</h2><div class="logo"></div></div></div></div></div></header>
简而言之,如何丑化 CodeIgniter View 的 HTML 代码?
提前致谢。
最佳答案
转到system/application/config/config.php
并检查是否启用了钩子(Hook):
$config['enable_hooks'] = TRUE;
在system/application/config/hooks.php
中声明新钩子(Hook):
// compress output
$hook['display_override'][] = array(
'class' => '',
'function' => 'compress',
'filename' => 'compress.php',
'filepath' => 'hooks'
);
在system/application/hooks/compress.php
中添加钩子(Hook):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function compress()
{
$CI =& get_instance();
$buffer = $CI->output->get_output();
$search = array(
'/\n/', // replace end of line by a space
'/\>[^\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'
);
$buffer = preg_replace($search, $replace, $buffer);
$CI->output->set_output($buffer);
$CI->output->_display();
}
/* End of file compress.php */
/* Location: ./system/application/hooks/compress.php */
来源:http://jeromejaglale.com/doc/php/codeigniter_compress_html
或者,您可以轻松地从以下位置安装此 Hook : https://github.com/johngerome/CodeIgniter-Minifyhtml-hooks#codeigniter-minifyhtml-hooks
关于php - 如何从 CodeIgniter 的 View 中替换制表符和新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377051/