做了个模板引擎 nging
发布于 10 年前 作者 srcjz 3494 次浏览 最后一次编辑是 8 年前 来自 分享

安装:

npm install nging

用法:

var app = require("express")();
var nging = require("nging");
 
function Comment() {
    this.jml = function() {
        return ["div",{className:"comment"},
                    ["h2",{className:"commentAuthor", style:this.props.style},
                        this.props.author
                    ]
               ].concat(this.props.nodes);
    };
}
 
function CommentList() {
    this.jml = function() {
        var commentNodes = this.props.data.map(function (comment) {
          return [Comment, {author:comment.author}, comment.text];
        });
 
        return ["div", {className:"commentList"}].concat(commentNodes);
    };
}
 
var comments = [
  {"author": "Pete Hunt", "text": "This is one comment"},
  {"author": "Jordan Walke", "text": "This is *another* comment"}
];
 
var jml1 = ["div",{class:"yes"},["p",{style:"color:red"},"Hello!!!",["img",{src:"http://i.imgur.com/gEZsVCW.png",width:500}]]];
var jml2 = ["html",["body",["div","dfe"],["span","ddd"]]];
 
var jml3 = [Comment, {author:"John", style:"color:red"}, "Hello!"];
 
var jml4 = ["html",jml3,jml3,jml3,jml3,jml3,jml3,jml3];
 
var jml5 =["form",{className:"commentForm", onSubmit:"this.handleSubmit"},
            ["input", {type:"text", placeholder:"Your name", ref:"author"}],
            ["input", {type:"text", placeholder:"Say something...", ref:"text"}],
            ["input", {type:"submit", value:"Post"}]
        ];
 
var jml6 =[CommentList, {data:comments}];
 
var jml7 = ["html", jml1,jml2,jml3,jml4,jml5];
 
app.get("/", function(req, res) {
    res.send(nging.render(jml7));
});
 
app.listen(8080);

回到顶部