2012-05-09 34 views
3

Supose我有這個簡單但還算嵌套生態模板移除不必要的空格:如何從編譯生態模板

<div class="example"> 
    <% for thing, i in @things: %> 
    <div class="nested"> 
     <% if i % 2 == 0: %> 
     This block is fairly nested. 
     <% end %> 
    </div> 
    <% end %> 
</div> 

當編譯成JS的結果是:

function(__obj) { 
    // ... A couple of auxiliary functions ... 
    (function() { 
    (function() { 
     var i, thing, _i, _len, _ref; 

     __out.push('<div class="example">\n '); 

     _ref = this.things; 
     for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { 
     thing = _ref[i]; 
     __out.push('\n <div class="nested">\n  '); 
     if (i % 2 === 0) { 
      __out.push('\n  This block is fairly nested.\n  '); 
     } 
     __out.push('\n </div>\n '); 
     } 

     __out.push('\n</div>\n'); 

    }).call(this); 

    }).call(__obj); 
    __obj.safe = __objSafe, __obj.escape = __escape; 
    return __out.join(''); 
} 

現在,這個功能(這是作爲JS客戶端做客戶端渲染)包括一些不必要的空白字符串,如...

`'\n  This block is fairly nested.\n  '` 

...不能被JS壓縮器刪除,因爲它們不是JS空格(但在呈現時變爲HTML空格)。據我瞭解,Eco以這種方式編譯模板以保持其輸出很好地縮進,這在開發環境中很酷,但在生產環境中並不那麼多:D

是否有方法從eco.precompile中移除這些不必要的空格輸出?

順便說一句,我使用鏈輪編譯,連接和服務這些資產。

+0

我還沒有使用生態環境,沒有一個可以嘗試任何東西的環境。你能看到是否在''中包含不需要的空白有幫助嗎? –

+0

感謝您的建議@AtesGoral,但不幸的是,這並沒有太大的幫助,因爲在JS輸出中看到的空格是因爲模板中使用的縮進(我編輯了我的問題評論)。在添加不必要的HTML註釋塊之前,我寧願刪除soruce模板的縮進,但這是遠遠不夠理想的解決方案(可能有一種方法可以在.eco - > .js編譯時刪除它們)。 – epidemian

+0

如果Eco尊重XML評論,它可能仍然有效。我會發布我的意思作爲答案。 –

回答

0

如果生態榮譽XML註釋,這可能幫助:

<div class="example"> 
    <% for thing, i in @things: %><!-- 
    --><div class="nested"><!-- 
     --><% if i % 2 == 0: %> 
     This block is fairly nested. 
     <% end %> 
    </div> 
    <% end %> 
</div> 

雖然,你將不得不作出這個醜陋或放棄壓痕醜陋的選擇。

+2

HTML註釋也包含在JS輸出中以及空白處。 Eco借用ERB的評論語法:'<%# %>'。那些評論不會出現在JS輸出中,但是用評論替換縮進對我來說是一種不行的方式 - 不是我正在尋找的= D – epidemian

+0

我聽到你的聲音:)但至少你有最後的使用方法再培訓局評論... –

2

,如果你把它寫這樣

<div class="example"> 
    <% for thing, i in @things: %> 
    <div class="nested"><%= "fairly nested" if i % 2 is 0 %></div> 
    <% end %> 
</div> 

的建議在ECO模板README什麼,大約空白記下。