4

I use phalconphp framework for my test project.

I have simple controller with following code:

$this->view->setVar('myData', $data);

Are there any tools in PhalconPHP to minify HTML code in view?

Thanks!

Sergey
  • 5,396
  • 3
  • 26
  • 38
  • 2
    I would suggest you do that on the webserver level. Google provides good tools to implement that in the webserver. – Michal Jul 16 '14 at 21:47
  • 1
    Nice idea. Use mod_pagespeed for that. This tool has many function to compress a website and it's resources! – Daniel K. Jul 16 '14 at 21:52

2 Answers2

1

At this point this is not yet possible built in. But have used this function to achieve the minify option.

function minifyHTML($content)
{
    $start = strpos( $content, '[#strip#]');

    if ($start > 0) {

        $end = strpos($content, '[#endstrip#]');

        $part =  substr($content,$start, $end - $start + 12);

        $content = str_replace($part,str_replace(array("\r", "\n", "\t" , 
            '[#strip#]', '[#endstrip#]'), '' , $part) , $content);

        if(strpos( $content, '[#strip#]') > 0)
            $content = stripHTML($content);
    }
    return $content;
}

Then in your volt file use it like this. But make sure to use tabs as indents instead of spaces in your volt file. Also [#strip#] tags are not allowed to be nested.

<!DOCTYPE html>
<html lang="nl">
<head>[#strip#]
    <meta charset="utf-8">
    {% block head %}
        {{ assets.outputCss() }}
    {% endblock %}
    <title>{% block title %}{% endblock %}</title>
[#endstrip#]</head>
<body>
{% block content %}
{% endblock %}
{% block script %}
    [#strip#]{{ assets.outputJs() }}[#endstrip#]
{% endif %}
{% endblock %}
</body>
</html>

And finally use it on your handle()->getContent().

$application = new \Phalcon\Mvc\Application($di);
echo minifyHTML( $application->handle()->getContent());
Parsaria
  • 11
  • 2
0

CSS and JavaScript is minified by built-in functions as you can see here: http://docs.phalconphp.com/en/latest/reference/assets.html#minification-filtering

It seems as if there is no tool for minifying HTML because it's not common to do this. But consider writing your own "minify"-function. There is not much to do because line breaks have no impact in HTML code and you can remove them in your output with something like this:

str_replace(array("\r\n", "\r"), "", $output);

Additionally - if you don't use more than one whitespace in your content - you can remove tabs, linebreaks and whitespaces like this:

preg_replace("/\s+/", " ", $string);
Daniel K.
  • 1,189
  • 1
  • 10
  • 26
  • I misread. Yeah -1 for me :-) Still converting to `\r` is strange. "Nothing" uses that. – PeeHaa Jul 16 '14 at 21:57
  • Well you are not that wrong, @PeeHaa. One example where line breaks also DO matter: http://jsfiddle.net/VNMs9/ So maybe using mod_pagespeed is the best solution after all. – Daniel K. Jul 16 '14 at 22:02