使用ejs和multiline 实现ES6 Template String的功能

jopen 10年前

es6TemplateString

使用ejs和multiline 实现ES6 Template String的功能

ES6 Template String 支持的功能

//Basic usage with an expression placeholder  var person = 'Hao Ju Zheng';  console.log(`Yo! My name is ${person}`);    //Expressions work just as well with object literals  var user = {name: 'Zhen Ou Yun'};  console.log(`Thanks, ${user.name}`);    //Expression interpolation, One use is readable inline math.  var a = 50;  var b = 100;  console.log(`The number of JS frameworks is ${a + b} and not ${ 2 * a +b }`);    //Multi-line strings without needing \n\  console.log(`string text line 1  string text line 2`);    //Function inside expressions  function fn() {return "result"}  console.log(`foo ${fn()} bar`);

浏览器支持情况

详见ES6兼容表

使用ejs和multiline的实现的es6TemplateString

//Basic usage with an expression placeholder  var person = "Hao Ju Zheng";    console.log(es6TemplateString(function(){/*!  Yo My name is ${= person }  */}, {'person': person}));      //Expressions work just as well with object literals  var user = {name: "Zhen Ou Yun"};    console.log(es6TemplateString(function(){/*!  Thanks, ${= name }  */}, user));      //Multi-line strings without needing \n\  console.log(es6TemplateString(function(){/*!  string text line 1  string text line 2  */}, {}));      //Function inside expressions  function fn() {return "result"}  console.log(es6TemplateString(function(){/*!  foo ${= fn() } bar  */}, {'fn': fn}));

使用es6TemplateString