2011-11-01 54 views
2
{strip} 
<div 
    class="x" 
> 
{/strip} 

成爲{strip}:如何避免意外刪除空白?

<divclass="x"> 

這是不是有人想什麼。

所以,問題:有沒有什麼辦法可以解決這個問題?想象的方法:

  • 用空格代替新線路,使用他們的論壇trimed

This topic未剝離的參數或其他智者,功能

  • 添加受保護的空間/沒有一個解決方案,(除了 - 添加您自己的自定義標籤)。另外,請不要在原始PHP或任何其他語言/框架中提供解決方案。

  • +0

    [It should not。](http://www.smarty.net/docs/en/language.modifier.strip.tpl) –

    +0

    @Tomalak Geret'kal a ** function ** [strip](http ://www.smarty.net/docs/en/language.function.strip.tpl),不是一個修飾符;) – c69

    +0

    親愛的@ c69,我編輯了你的問題,因爲我遇到了同樣的問題,但「把新行變成了空格「並沒有出現在我的腦海中作爲一個搜索查詢。希望沒關係。 –

    回答

    4

    您可以用@開發的方法和capture你的數據去,並通過strip modifier運行:

    {capture name="spaces"} 
    <div 
        class="x" 
    > ... </div> 
    {/capture} 
    {$smarty.capture.spaces|strip:" "} 
    

    或運行通過採集內容regex_replace modifier(本質上與拆分相同,但具有更多開銷):

    {$smarty.capture.spaces|regex_replace:"#\s+#":" "} 
    

    或下降一個新的名爲custom block plugin是trimwhitespace利用了outputfilter trimwhitespace的:

    <?php 
    function smarty_block_trimwhitespace($params, $content, Smarty_Internal_Template $template, &$repeat) 
    { 
        require_once SMARTY_PLUGINS_DIR . 'outputfilter.trimwhitespace.php'; 
        return smarty_outputfilter_trimwhitespace($content, $template->smarty); 
    } 
    

    調用這個文件block.trimwhitespace.php,並將其放置在插件目錄。用它在你的模板:

    {trimwhitespace} 
    <div 
        class="x" 
    > ... </div> 
    {/trimwhitespace} 
    

    雖然這兩種方法修改將工作的優良簡單的HTML的東西,他們會打破內容包括<script><pre>標籤。如果你需要這些,你想使用包裝的輸出過濾器。

    如果您希望所有輸出都通過該篩選器運行,請不要更改您的模板並將$smarty->loadFilter('output', 'trimwhitespace');添加到您的設置中。

    +0

    tnx精心製作答案! – c69

    1

    代碼分配給一個變量,並嘗試{$articleTitle|strip:'&nbsp;'}

    3

    保護個人空間:

    {strip} 
    <div class="first{" "} 
        {"second "} 
        third"> 
    {/strip} 
    

    成爲

    <div class="first second third"> 
    

    工作正常,我使用Smarty V3。

    +0

    也適用於Smarty v2。 –

    +0

    謝謝,你是否知道是否可以避免在每個模板中執行它,而是在'header中頂部。tpl'並在'footer.tpl'的底部。看來Smarty標籤不是通過模板傳輸的? –