UNPKG

lite

Version:

A cross platform template engine base on xml/html and javascript expression.

65 lines (60 loc) 1.86 kB
/* * List Template * License LGPL(您可以在任何地方免费使用,但请不要吝啬您对框架本身的改进) * http://www.xidea.org/project/lite/ * @author jindw * @version $Id: template.js,v 1.4 2008/02/28 14:39:06 jindw Exp $ */ /** * @param data template source(xml source|| dom) * @param config {params:params,liteImpl:'liteImpl'} * @public */ function parseLite(data,config){ var root = config&&config.root; var path = config&&config.path; if(!path){ if(typeof data == 'string' && /^\//.test(data)){ path = data; }else{ path = data.documentURI;//dom } } root = root || String(path).replace(/[^\/\\]+$/,''); var parseContext = new ParseContext(root && new ParseConfig(root)); path && parseContext.setCurrentURI(path) if(typeof data == 'string' && /^\s*<|>\s*$/.test(data)){ //console.log(path,parseContext.currentURI) data = parseContext.loadXML(data); }else{ parseContext.setCurrentURI(parseContext.createURI(path)) //console.log(parseContext.currentURI) } parseContext.parse(data); try{ if(config instanceof Array){ config = {params:config} } var translator = new JSTranslator(); //translator.liteImpl = "lite_impl" //console.log(parseContext.toList()) var code = translator.translate(parseContext.toList(),config); //console.log(code) data = new Function('return '+code).apply(); data.toString=function(){//_$1 encodeXML return code; } return data; }catch(e){ console.error("translate error",e,code) throw e; } } if(typeof require == 'function'){ exports = module.exports = require('./lite-engine').LiteEngine exports.parseLite=parseLite; exports.LiteEngine=exports; var ParseConfig=require('./parse/config').ParseConfig; var JSTranslator=require('./parse/js-translator').JSTranslator; var ParseContext=require('./parse/parse-context').ParseContext; }