将整个Smarty模板包装在{strip}标签中有什么问题吗?

Wro*_*ngs 3 html php smarty minify

我希望我的模板保持整洁和良好的意图,但我想只向浏览器提供非常紧凑的HTML.
错过了一个更好的主意,我想知道将整个Smarty模板包装在这样的{strip}标签中是否有问题?

{strip}
        <div class="albums">
            <h2>Alle Foto-Alben</h2>

            <ul class="photos clearfix">
                {foreach $albums as $album}
                    {if $album->publishedPhotos|count > 0}
                    <li>
                        <div>
                            <a href="album.php?id={$album->id}">
                                <img src="{$album->titlepic->thumb}" alt="{$album->titlepic->title}" />
                                <span class="title">{$album->title}</span>
                                <span class="counter">{$album->publishedPhotos|count} Foto{if $album->publishedPhotos|count != 1}s{/if}</span>
                            </a>
                        </div>
                    </li>
                    {/if}
                {/foreach}
            </ul>
        </div>
{/strip}
Run Code Online (Sandbox Code Playgroud)

它闻起来有点不专业,但我无法想出更好的东西.
一个缺点肯定是你必须在这些标签中包装每一个模板.

我很高兴得到纠正,并希望听到不同的方法来保持交付的代码紧凑.

Lin*_*äck 5

虽然没有错,但我会建议使用预过滤器.预过滤器仅在模板编译时运行(因此不会减慢服务器速度),并且您不需要在{strip}中包装每个模板.以下行代码来自我正在处理的项目.它有效地剥离了大多数空白.

/* Minify the html */
function smarty_pre_minify($tpl_source, $smarty) { return preg_replace('/[ \t\n\r]+/s', ' ', $tpl_source); }
$smarty->registerFilter('pre', 'smarty_pre_minify');
Run Code Online (Sandbox Code Playgroud)