John Resig Micro templating bug

So I am aiming to use John Resig's templating engine.

I need to pass the function a variable containing the "template". However, the problem is that I need to pass something like:

<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>
<% } %>

The thing that seems to break is:

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

With this bit of code being passed through the Regex it fails. It is important that I am able to pass any of the above code and have it not break. Any ideas?

Answers


So I came across this article which has a fix for this (also changes the <% %> to <# #> 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


Need Your Help

SQL Joins with distinct columns

sql db2

Consider two (possibly temporary) tables X and Y each with many columns. In DB2, if I make a cross join like this,

How do I add a data-offset to a Bootstrap 4 fixed-top responsive navbar?

navigation navbar nav bootstrap-4 twitter-bootstrap-4

So, in Bootstrap v3, you would just add a data-offset attribute to the nav tag, with the pixel offset, in this case 54px,