简体   繁体   中英

output template string in a new line

When I try to append \\n to my template string , it does not create a new line. I'm not sure where the code is behaving in-correctly.

Below is my template String :

var template = 
'My skills: \n' + 
'<%if(this.showSkills) {%>' +
    '<%for(var index in this.skills) {%>' + 
    '<a href="#"><%this.skills[index]%></a> \n' +
    '<%}%>' +
'<%} else {%>' +
    '<p>none</p> \n' +
'<%}%> \n';
console.log(TemplateEngine(template, {
    skills: ["js", "html", "css"],
    showSkills: true
}));

Template Engine function

var TemplateEngine = function(html, options) {
    var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0, match;
    var add = function(line, js) {
        js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
            (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
        return add;
    }
    while(match = re.exec(html)) {
        add(html.slice(cursor, match.index))(match[1], true);
        cursor = match.index + match[0].length;
    }
    add(html.substr(cursor, html.length - cursor));
    code += 'return r.join("");';
    return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
}

Below is the output:

My skills:   <a href="#">js</a>  <a href="#">html</a>  <a href="#">css</a>

Here is a working fiddle :

http://jsfiddle.net/nrd2ktxn/

I want each of output string in a new line like this:

My skills:   
<a href="#">js</a>  
<a href="#">html</a>  
<a href="#">css</a>   

That's because you just add quotes around the input text and escape inner quotes:

'r.push("' + line.replace(/"/g, '\\"') + '")'

But if line contains line terminators , it will produce invalid JS which will throw when you eval it.

Effectively, removing all line breaks in the code solves it:

code.replace(/[\r\t\n]/g, '')

However, it gets rid of the line breaks in the input text.

Instead, you should handle line terminators. Something like

'r.push("' + line
  .replace(/\\/g, '\\\\')        // Escape the escaping character
  .replace(/"/g, '\\"')          // Escape double quotes
  .replace(/\n/g, '\\n')         // Escape <LF>
  .replace(/\r/g, '\\r')         // Escape <CR>
  .replace(/\u2028/g, '\\u2028') // Escape <LS>
  .replace(/\u2029/g, '\\u2029') // Escape <PS>
+ '");'

 var TemplateEngine = function(html, options) { var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\\n', cursor = 0, match; function escapeText(s) { return '"' + s .replace(/\\\\/g, '\\\\\\\\') .replace(/"/g, '\\\\"') .replace(/\\n/g, '\\\\n') .replace(/\\r/g, '\\\\r') .replace(/\
/g, '\\\
') .replace(/\
/g, '\\\
') + '"'; } var add = function(line, js) { js? (code += line.match(reExp) ? line + '\\n' : 'r.push(' + line + ');\\n') : (code += line != '' ? 'r.push(' + escapeText(line) + ');\\n' : ''); return add; } while(match = re.exec(html)) { add(html.slice(cursor, match.index))(match[1], true); cursor = match.index + match[0].length; } add(html.substr(cursor, html.length - cursor)); code += 'return r.join("");'; return new Function(code).apply(options); } var template = 'My skills: \\n' + '<%if(this.showSkills) {%> \\n' + '<%for(var index in this.skills) {%> \\n' + '<a href="#"><%this.skills[index]%></a> \\n' + '<%}%> \\n' + '<%} else {%> \\n' + '<p>none</p> \\n' + '<%}%> \\n'; console.log(TemplateEngine(template, { skills: ["js", "html", "css"], showSkills: true })); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM