如何让node.js使用CFML风格的#hashed#变量
How do I get node.js to use CFML-style #hashed# variables?
我正在构建一个 Node.js 框架,我喜欢 ColdFusion 的<cfoutput>
风格的哈希限定变量。
我正在尝试研究如何达到相同的效果。例如:
<h1>
#this.pageTitle#
</h1>
<div>
#this.content()#
</div>
从我在上面主要问题中留下的评论中。
/**
* Load the Virtual Machine
*/
var vm = require('vm');
/**
* Template Data
*/
var template = "<h1>#this.content#</h1><div>#this.sitename()#</div>";
/**
* Process method
*/
function compile(source)
{
var __ = "this.__compiled = '";
/**
* Replace all template tags
*/
__ += source.replace(/'#(.*?)'#/g, "' +$1+ '");
return __ + "';";
}
/**
* Create the context / scope, this can be anything from 'this', 'process' to 'require('fs')'
*/
var context = {
content : "Robert Pitt",
sitename : function(){
return "http://robertpitt.me";
}
};
/**
* Compile the code within the sandbox
*/
var compiled = vm.runInNewContext(compile(template), context);
/**
* Use compiled source:
* value: <h1>Robert Pitt</h1><div>http://robertpitt.me</div>
*/
console.log(compiled);
CoffeeScript 有字符串插值:
author = "Wittgenstein"
quote = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"
相关文章:
- 为什么Airbnb风格指南说不鼓励依赖函数名称推断
- CKEditor-我在editor.css中的风格是't
- onclick风格的整个李不想要
- JavaScript改变了双方的显示风格
- 如何移除或改进:在Bootstrap Carousel上聚焦风格
- 如何塑造李的风格;s正在使用单击的李的值
- 用于Dynamic Div'的Javascript数学函数;s风格
- 创建一个不断变化的谷歌涂鸦风格的标题
- jQuery将TD注入适当的列(俄罗斯方块风格)
- 最好的技术自上而下RPG风格的网格滚动
- 在Windows Metro风格的Javascript应用程序中读取文件内容
- jquery可以't隐藏输入名称为“”的表单;风格”;
- ACF谷歌地图-改变风格
- system.err风格的控制台以javascript打印
- 野蛮风格搞砸了
- 带有jquery/javascript的桌面风格UI
- 我可以用JavaScript判断访问者是否'的语言环境遵循美国风格或世界其他地区's风格
- 一些sencha/enyo风格功能的主干兼容UI/组件库
- Javascript-如何将类工厂定义为Extjs风格
- 如何让node.js使用CFML风格的#hashed#变量