【问题标题】:John Resig Micro templating bugJohn Resig Micro 模板错误
【发布时间】:2014-04-10 17:06:58
【问题描述】:

所以我的目标是使用John Resig's templating engine

我需要向函数传递一个包含“模板”的变量。但是,问题是我需要传递类似的东西:

<script>
console.log("Double quotes");
</script>
<script>
console.log('single');
</script>
<iframe src="http://example.com/?<%=SOME_VARIABLE%>" frameborder="0"></iframe>
<div>I think I'm going to break</div>
<% for ( var i = 0; i < users.length; i++ ) { %>
<li><a href="<%=users[i]%>"><%=users[i]%></a></li>
<% } %>
<iframe src='http://example.com/?<%=SOME_VARIABLE%>' frameborder="0"></iframe>
<div>I think I"m going to break</div>
<% for ( var i = 0; i < users.length; i++ ) { %>
<li><a href='<%=users[i]%>'><%=users[i]%></a></li>
<% } %>

似乎打破的是:

<script>
console.log('single');
</script>

通过正则表达式传递这段代码会失败。重要的是我能够传递上述任何代码并且不会中断。有什么想法吗?

【问题讨论】:

    标签: javascript regex client-side-templating templating-engine


    【解决方案1】:

    所以我遇到了这篇文章,它对此进行了修复(也将 更改为 FYI

    str.replace(/[\r\t\n]/g, " ")
                   .replace(/'(?=[^#]*#>)/g, "\t")
                   .split("'").join("\\'")
                   .split("\t").join("'")
                   .replace(/<#=(.+?)#>/g, "',$1,'")
                   .split("<#").join("');")
                   .split("#>").join("p.push('")
                   + "');}return p.join('');";
    

    http://weblog.west-wind.com/posts/2008/Oct/13/Client-Templating-with-jQuery

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多