admin管理员组文章数量:1023827
I'm having a bit of trouble with John Resig's Micro templating.
Can anyone help me with why it isn't working?
This is the template
<script type="text/html" id="row_tmpl">
test content {%=id%} {%=name%}
</script>
And the modified section of the engine
str
.replace(/[\r\t\n]/g, " ")
.split("{%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
and the javascript
var dataObject = { "id": "27", "name": "some more content" };
var html = tmpl("row_tmpl", dataObject);
and the result, as you can see =id and =name seem to be in the wrong place? Apart from changing the template syntax blocks from <% %> to {% %} I haven't changed anything.
This is from Firefox.
Error: syntax error
Line: 30, Column: 89
Source Code:
var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(' test content ');=idp.push(' ');=namep.push(' ');}return p.join('');
I'm having a bit of trouble with John Resig's Micro templating.
Can anyone help me with why it isn't working?
This is the template
<script type="text/html" id="row_tmpl">
test content {%=id%} {%=name%}
</script>
And the modified section of the engine
str
.replace(/[\r\t\n]/g, " ")
.split("{%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
and the javascript
var dataObject = { "id": "27", "name": "some more content" };
var html = tmpl("row_tmpl", dataObject);
and the result, as you can see =id and =name seem to be in the wrong place? Apart from changing the template syntax blocks from <% %> to {% %} I haven't changed anything.
This is from Firefox.
Error: syntax error
Line: 30, Column: 89
Source Code:
var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(' test content ');=idp.push(' ');=namep.push(' ');}return p.join('');
Share
Improve this question
edited Apr 16, 2010 at 10:37
Chris Barry
asked Apr 15, 2010 at 22:36
Chris BarryChris Barry
4,5947 gold badges57 silver badges90 bronze badges
1
- I got really confused when I got to the words, "the modified section of the engine." – Pointy Commented Apr 15, 2010 at 22:42
2 Answers
Reset to default 6To change the tags in John Resig's Micro-Template Library, you must change two split() calls and the two regular expressions from the original code.
For example, changing the original tags <%= %> to {{= }} is done like so
//ORIGINAL
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t") //CHANGE string "<%"
.replace(/((^|%>)[^\t]*)'/g, "$1\r") //CHANGE expression /((^|%>)[^\t]*)'/g
.replace(/\t=(.*?)%>/g, "',$1,'") //CHANGE expression /\t=(.*?)%>/g
.split("\t").join("');")
.split("%>").join("p.push('") //CHANGE string "%>"
.split("\r").join("\\'")
//INTO
str
.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t") //INTO string "{{"
.replace(/((^|\}\})[^\t]*)'/g, "$1\r") //INTO expression /((^|\}\})[^\t]*)'/g
.replace(/\t=(.*?)\}\}/g, "',$1,'") //INTO expression /\t=(.*?)\}\}/g
.split("\t").join("');")
.split("}}").join("p.push('") //INTO string "}}"
.split("\r").join("\\'")
Be carefull when changing regular expressions in the engine, you must escape special characteres. In javascript flavor of regexp, these are: \ / [ ] ( ) { } ? + * | . ^ $ Escape them by prefixing them with "\". This does not apply to the split params as they are not regexp in this case.
When you modified the engine, you forgot to change these lines:
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
to this:
.replace(/((^|%\})[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%\}/g, "',$1,'")
I'm having a bit of trouble with John Resig's Micro templating.
Can anyone help me with why it isn't working?
This is the template
<script type="text/html" id="row_tmpl">
test content {%=id%} {%=name%}
</script>
And the modified section of the engine
str
.replace(/[\r\t\n]/g, " ")
.split("{%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
and the javascript
var dataObject = { "id": "27", "name": "some more content" };
var html = tmpl("row_tmpl", dataObject);
and the result, as you can see =id and =name seem to be in the wrong place? Apart from changing the template syntax blocks from <% %> to {% %} I haven't changed anything.
This is from Firefox.
Error: syntax error
Line: 30, Column: 89
Source Code:
var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(' test content ');=idp.push(' ');=namep.push(' ');}return p.join('');
I'm having a bit of trouble with John Resig's Micro templating.
Can anyone help me with why it isn't working?
This is the template
<script type="text/html" id="row_tmpl">
test content {%=id%} {%=name%}
</script>
And the modified section of the engine
str
.replace(/[\r\t\n]/g, " ")
.split("{%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
and the javascript
var dataObject = { "id": "27", "name": "some more content" };
var html = tmpl("row_tmpl", dataObject);
and the result, as you can see =id and =name seem to be in the wrong place? Apart from changing the template syntax blocks from <% %> to {% %} I haven't changed anything.
This is from Firefox.
Error: syntax error
Line: 30, Column: 89
Source Code:
var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(' test content ');=idp.push(' ');=namep.push(' ');}return p.join('');
Share
Improve this question
edited Apr 16, 2010 at 10:37
Chris Barry
asked Apr 15, 2010 at 22:36
Chris BarryChris Barry
4,5947 gold badges57 silver badges90 bronze badges
1
- I got really confused when I got to the words, "the modified section of the engine." – Pointy Commented Apr 15, 2010 at 22:42
2 Answers
Reset to default 6To change the tags in John Resig's Micro-Template Library, you must change two split() calls and the two regular expressions from the original code.
For example, changing the original tags <%= %> to {{= }} is done like so
//ORIGINAL
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t") //CHANGE string "<%"
.replace(/((^|%>)[^\t]*)'/g, "$1\r") //CHANGE expression /((^|%>)[^\t]*)'/g
.replace(/\t=(.*?)%>/g, "',$1,'") //CHANGE expression /\t=(.*?)%>/g
.split("\t").join("');")
.split("%>").join("p.push('") //CHANGE string "%>"
.split("\r").join("\\'")
//INTO
str
.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t") //INTO string "{{"
.replace(/((^|\}\})[^\t]*)'/g, "$1\r") //INTO expression /((^|\}\})[^\t]*)'/g
.replace(/\t=(.*?)\}\}/g, "',$1,'") //INTO expression /\t=(.*?)\}\}/g
.split("\t").join("');")
.split("}}").join("p.push('") //INTO string "}}"
.split("\r").join("\\'")
Be carefull when changing regular expressions in the engine, you must escape special characteres. In javascript flavor of regexp, these are: \ / [ ] ( ) { } ? + * | . ^ $ Escape them by prefixing them with "\". This does not apply to the split params as they are not regexp in this case.
When you modified the engine, you forgot to change these lines:
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
to this:
.replace(/((^|%\})[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%\}/g, "',$1,'")
本文标签:
版权声明:本文标题:javascript - Syntax Error with John Resig's Micro Templating after changing template tags <# {% {{ etc - Stack Ov 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745593184a2158007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论