反编译_underscore模板
decompile _underscore template
我有很多 "compiled" 下划线模板(几个月前我将编译后的模板保存到文件中,不小心删除了原始模板文件夹...:( ) "decompile"这个模板?示例之一:
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))
},
您可以手动反编译它。根据您的代码,您的模板定义将是:
`<div class="button" data-id="<%= data._id %>'">
<div class="icon"></div>
</div>`
这很容易,但对于复杂的模板,它会更难。
如果您阅读 source of _.template
, you'll find it's simple enough that you could reverse it with a few hours of work. Make sure to find the code for your version of underscore (clearly yours isn't the most recent as there are changes), old docs can be found in the 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, "<%- %>");
$('#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"/>
我有很多 "compiled" 下划线模板(几个月前我将编译后的模板保存到文件中,不小心删除了原始模板文件夹...:( ) "decompile"这个模板?示例之一:
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))
},
您可以手动反编译它。根据您的代码,您的模板定义将是:
`<div class="button" data-id="<%= data._id %>'">
<div class="icon"></div>
</div>`
这很容易,但对于复杂的模板,它会更难。
如果您阅读 source of _.template
, you'll find it's simple enough that you could reverse it with a few hours of work. Make sure to find the code for your version of underscore (clearly yours isn't the most recent as there are changes), old docs can be found in the 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, "<%- %>");
$('#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"/>