【问题标题】:Is something wrong with wrapping whole Smarty templates in {strip} tags?将整个 Smarty 模板包装在 {strip} 标签中是否有问题?
【发布时间】:2010-08-10 00:18:03
【问题描述】:

我想让我的模板保持整洁和良好的意图,但只想向浏览器提供非常紧凑的 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}

这对我来说有点不专业,但我想不出更好的东西。
一个缺点肯定是您必须将每一个模板包装在这些标签中。

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

【问题讨论】:

    标签: php html smarty minify


    【解决方案1】:

    虽然没有错,但我建议改用预过滤器。预过滤器仅在模板编译时运行(因此它不会减慢服务器速度)并且您不需要将每个模板包装在 {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');
    

    【讨论】:

    • 所以用{strip}包裹一切都不行?
    【解决方案2】:

    不,这没有什么问题。

    在一对页眉/页脚模板中执行此操作,而不是单独包装每个页面。

    或者,您可以在 PHP 方面触发剥离。

    【讨论】:

    • 感谢您的回答。但是,页眉/页脚不起作用。我正在单独显示这些模板,因此head.tpl 的 start-{strip} 不再在index.tpl 中编译(或者我错了)? $smarty->display('_inc/tpl.head.tpl'); $smarty->display('index.tpl'); $smarty->display('_inc/tpl.foot.tpl');
    • @Matze:不,你是对的,如果它们在单独的独立显示的模板中,{strip} 将不起作用。不过,您可以在显示之前将模板组合在一起,然后就可以了。
    • 感谢您的回答;你是对的。但是,我将如何在 PHP 方面调用 Smaty 的 strip? (我正在使用 Slim 框架)。
    【解决方案3】:

    注意: Striptags 将忽略包含。当天免费小费。 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      相关资源
      最近更新 更多