Node.js中有类似于Python中“字符串模板”的功能么……
 发布于 11 年前  作者 friskit-china  9569 次预览  最后一次回复是 10 年前  来自  

貌似“字符串模板”这个术语不太准确。。。。

a = {'name':'World'}
str = 'Hello %(name)s!'
print str%a # Hello World!

总感觉直接用ejs之类的模板系统太重了。。。有没有轻量化一些的解决方案

12 回复
baryon

在nodejs里,这种模板是如此之多 比较有名的是mustache https://github.com/janl/mustache.js ,以及他的强化版:handlebars http://handlebarsjs.com/

检索一下,会有很多

alsotang

下面都是 atpl dust eco ect ejs haml haml-coffee handlebars hogan jade jazz jqtpl JUST liquor lodash mustache nunjucks QEJS ractive swig templayed toffee underscore walrus whiskers

friskit-china

— —。。。大家还没明白我的意思……我是觉得这样的模板系统太重了……有没有一些微量级的解决方案,我不是想要一个html模板……只是要在字符串中插入一些内容而已……

youxiachai

需求是微量级的话,上面那个文档里头的直接用字符位置锚点,应该可以满足吧?

friskit-china

恩。。这个靠谱……多谢~

rapidhere

一直觉得underscore应该加一个format功能

friskit-china

点赞。。。大家快去fork吧。。。

louislve

有的

看http://www.csdn.net/article/2015-09-15/2825708-7-reasons-to-upgrade-to-node-v4?utm_source=tuicool

《升级到Node V4的七个理由 支持大量ES6特性》

Node V4就有增加模板字符串功能

WilliamDu1981

es6就有模版字符串了

backsapce

ES6有Template strings,nodejs 4.0+支持,其他版本加flag ES6 Template strings