在新行中输出模板字符串
output template string in a new line
当我尝试将'n
附加到模板string
时,它不会创建新行。我不确定code
的行为是否正确。
下面是我的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
引擎功能
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);
}
输出如下:
My skills: <a href="#">js</a> <a href="#">html</a> <a href="#">css</a>
这是一个工作fiddle
:
我想要每个output
字符串在一个新的行,像这样:
My skills:
<a href="#">js</a>
<a href="#">html</a>
<a href="#">css</a>
这是因为您只需在输入文本周围添加引号并转义内部引号:
'r.push("' + line.replace(/"/g, '''"') + '")'
但是如果line
包含行终止符,它将产生无效的JS,当你求值时将抛出。
code.replace(/['r't'n]/g, '')
但是,它去掉了输入文本中的换行符。
相反,您应该处理行终止符。就像'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(/'u2028/g, '''u2028')
.replace(/'u2029/g, '''u2029')
+ '"';
}
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
}));
相关文章:
- 如何将angularjs中的javascript字符串输出为循环数组
- Javascript将数组转换为字符串并使其反向输出
- 以字符串形式输出Javascript中的日期
- 编辑输出字符串函数ol.control.MouseControl
- 输出包含3个单词的字符串中的前2个单词
- 如何转义php中的新行字符以输出为javascript字符串值
- jQuery从选项的输出字符串中选择HTML选项
- 如何通过JavaScript将二进制(输入)转换为字符串(输出)
- 字符串输出中的 JavaScript 变量
- js 错误的字符串输出
- 如何将数字2.00作为一个完整的字符串输出
- Javascript -使用字符串输出HTML
- 递归字符串输出
- 如何在javascript中从xml字符串输出特定xml元素的所有值
- PHP json编码-数组键不作为字符串输出
- 浏览器和服务器javascript中的十六进制字符串输出不同
- 使用javaScript将字符串输出到HTML
- 以 ng 重复显示字符串输出
- Javascript for循环计数器作为字符串输出
- 设计一个Javascript对象,在每个新的Date()中作为字符串输出到console.log