【前端】制作一个handlebars的jQuery插件

(function($) {
    var compiled = {};
    $.fn.handlebars = function($srcNode, data) {
        // 取出模版内容
        var src = $srcNode.html();
        // 编译模版
    	var template = Handlebars.compile(src);
        // 传入数据,得到html并显示
    	this.html(template(data));
    };
})(jQuery);

// 使用
$('#content').handlebars($('#template'), { name: "Alan" });
posted @ 2016-05-26 10:11  赵康  阅读(181)  评论(0编辑  收藏  举报