2017-01-22 122 views
0

我有很多「編譯」下劃線模板(幾個月前我將編譯好的模板保存到文件上,並意外刪除原始模板文件夾...... :()是否可以「反編譯」這個模板?例如之一:反編譯_underscore模板

UV.templates["template-button-widget"] = function() { 
     return $.trim(function(obj) { 
      var __t, __p = "", 
       __j = Array.prototype.join, 
       print = function() { 
        __p += __j.call(arguments, "") 
       }; 
      with(obj || {}) __p += '\n\n <div class="button" data-id="' + (null == (__t = data._id) ? "" : _.escape(__t)) + '">\n\n  <div class="icon"></div>\n\n </div>\n\n'; 
      return __p 
     }.apply(this, arguments)) 
}, 
+0

,如果您使用的git - 通常你可以做恢復到承諾在您刪除的文件夾。 –

+0

不要使用git這個項目,但謝謝 – 20yco

回答

1

如果您閱讀了_.templatesource,您會發現它很簡單,您可以在幾個小時的工作中將其反轉。請確保找到您的下劃線版本的代碼(顯然,由於存在更改,您的代碼不是最新的),舊文檔可以在changelog中找到。

這裏是扭轉你的示例模板所需的代碼:

var compiled = function() { 
 
     return $.trim(function(obj) { 
 
      var __t, __p = "", 
 
       __j = Array.prototype.join, 
 
       print = function() { 
 
        __p += __j.call(arguments, "") 
 
       }; 
 
      with(obj || {}) __p += '\n\n <div class="button" data-id="' + (null == (__t = data._id) ? "" : _.escape(__t)) + '">\n\n  <div class="icon"></div>\n\n </div>\n\n'; 
 
      return __p 
 
     }.apply(this, arguments)) 
 
}; 
 

 
var source = compiled.toString(); 
 

 
// Strip start/end code 
 
source = source.substring(source.indexOf("with(obj || {}) __p += '\\n\\n") + 28); 
 
source = source.substring(0, source.indexOf("\\n\\n';")); 
 

 
// Reverse escape 
 
source = source.replace(/' \+ \(null == \(__t = ([^)]+)\) \? "" : _.escape\(__t\)\) \+ '/g, "<%- $1 %>"); 
 

 
$('#result').text(source.replace(/\\n/g, "\n"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script> 
 
<pre id="result"/>

+0

聖莫爾! huuuuugggeee謝謝! – 20yco

0

您可以手動編譯吧..你的代碼庫的模板定義是:

`<div class="button" data-id="<%= data._id %>'"> 
    <div class="icon"></div> 
</div>` 

很容易,但對於複雜的模板將變得更加困難

+0

謝謝,但是,這是最簡單的模板,我有超過50個模板與「硬代碼」 – 20yco