如何删除Smarty中标签之间的所有空白space?

How can to remove all blank space between tags in Smarty?

假设我们在编辑器中有这段代码:

<div class="menu">
  <div>Hello 1</div>
  <div>Hello 2</div>
  <div>Hello 3</div>
</div>

如何删除 html 标签之间的所有空格?所以结果应该是:

<div class="menu"><div>Hello 1</div><div>Hello 2</div><div>Hello 3</div</div>

使用{strip}

{strip}
<div class="menu">
  <div>Hello 1</div>
  <div>Hello 2</div>
  <div>Hello 3</div>
</div>
{/strip}

如果你想在同一行中删除 spaces,这有点复杂,你必须使用捕获并进行正则表达式替换(或为此创建你自己的插件) :

{capture name="code"}
{strip}
<div id="1"></div> <div id="2"></div>
{/strip}
{/capture}
{$smarty.capture.code|regex_replace:'/\>[\s\r\t\n]*\</':'><'}

然而,当标签之间应该存在 space 时,这会产生意想不到的结果。考虑即:

<strong>one hundred</strong> <span class="unit">grams</span>

删除 space 将使 html 呈现为

一百

所以我通过自定义块功能解决了这个问题,我使用以下代码创建了新的 PHP 文件 "smarty/plugins/block.minify.php"

function smarty_block_minify($params, $content, &$smarty, &$repeat) {
    if (!$repeat) {
        if (isSet($content)) {
            // HTML Minifier
            $input = $content;
            if(trim($input) === "") return $input;
            // Remove extra white-space(s) between HTML attribute(s)
            $input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
                return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?))?(\s+|$)#s', ' ', $matches[2]) . $matches[3] . '>';
            }, str_replace("\r", "", $input));
            // Minify inline CSS declaration(s)
            if(strpos($input, ' style=') !== false) {
                $input = preg_replace_callback('#<([^<]+?)\s+style=([\'"])(.*?)(?=[\/\s>])#s', function($matches) {
                    return '<' . $matches[1] . ' style=' . $matches[2] . minify_css($matches[3]) . $matches[2];
                }, $input);
            }
            return preg_replace(
                array(
                    // t = text
                    // o = tag open
                    // c = tag close
                    // Keep important white-space(s) after self-closing HTML tag(s)
                    '#<(img|input)(>| .*?>)#s',
                    // Remove a line break and two or more white-space(s) between tag(s)
                    '#(<!--.*?-->)|(>)(?:\n*|\s{2,})(<)|^\s*|\s*$#s',
                    '#(<!--.*?-->)|(?<!\>)\s+(<\/.*?>)|(<[^\/]*?>)\s+(?!\<)#s', // t+c || o+t
                    '#(<!--.*?-->)|(<[^\/]*?>)\s+(<[^\/]*?>)|(<\/.*?>)\s+(<\/.*?>)#s', // o+o || c+c
                    '#(<!--.*?-->)|(<\/.*?>)\s+(\s)(?!\<)|(?<!\>)\s+(\s)(<[^\/]*?\/?>)|(<[^\/]*?\/?>)\s+(\s)(?!\<)#s', // c+t || t+o || o+t -- separated by long white-space(s)
                    '#(<!--.*?-->)|(<[^\/]*?>)\s+(<\/.*?>)#s', // empty tag
                    '#<(img|input)(>| .*?>)<\/\x1A>#s', // reset previous fix
                    '#(&nbsp;)&nbsp;(?![<\s])#', // clean up ...
                    // Force line-break with `&#10;` or `&#xa;`
                    '#&\#(?:10|xa);#',
                    // Force white-space with `&#32;` or `&#x20;`
                    '#&\#(?:32|x20);#',
                    // Remove HTML comment(s) except IE comment(s)
                    '#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^!])#s'
                ),
                array(
                    "<</\x1A>",
                    '',
                    '',
                    '',
                    '',
                    '',
                    '<',
                    ' ',
                    "\n",
                    ' ',
                    ""
                ),
                $input);

            return $input;
        }
    }
}

现在我可以通过 {minify}some html code{/minify} 调用它了。

如果您无法进入Smarty的安装目录安装上述插件,您可以进行以下操作:

<!-- "strip" removes line breaks -->
{capture name="nospacesbetweentags"}
{strip}
    <div class="row">
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
    </div>
{/strip}
{/capture}
<!-- this removes spaces & tabs between tags -->
{$smarty.capture.nospacesbetweentags|replace:"> <":"><"|replace:">  <":"><"}

我不知道替换正则表达式是否更好,但这对我来说很好。

编辑: {strip} 不适用于包含的子模板 ({strip}{include "..."}{/strip})。因此,|strip修饰符是必要的:

{capture name="nospacesbetweentags"}
    <div id="row1" class="row">
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
        <div class="col-md-4 col-sm-6">
            Lorem ipsum dolor sit amet consectetur
        </div>
    </div>
    {include "row2.tpl"}
{/capture}
<!-- |strip already modifies multiple tabs into a sigle space -->
{$smarty.capture.nospacesbetweentags|strip|replace:"> <":"><"}