码迷,mamicode.com
首页 > 其他好文 > 详细

artTemplate模板引擎

时间:2016-07-22 14:33:12      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

源代码地址https://codeload.github.com/aui/artTemplate/zip/master

//定义模板引擎开始标签和结束标签
defaults.openTag = ‘{{‘;
defaults.closeTag = ‘}}‘;

var filtered = function (js, filter) {
    var parts = filter.split(‘:‘);
    var name = parts.shift();
    var args = parts.join(‘:‘) || ‘‘;

    if (args) {
        args = ‘, ‘ + args;
    }

    return ‘$helpers.‘ + name + ‘(‘ + js + args + ‘)‘;
}


defaults.parser = function (code, options) {

    // var match = code.match(/([\w\$]*)(\b.*)/);
    // var key = match[1];
    // var args = match[2];
    // var split = args.split(‘ ‘);
    // split.shift();

    code = code.replace(/^\s/, ‘‘);

    var split = code.split(‘ ‘);
    var key = split.shift();
    var args = split.join(‘ ‘);

    

    switch (key) {

        case ‘if‘:

            code = ‘if(‘ + args + ‘){‘;
            break;

        case ‘else‘:
            
            if (split.shift() === ‘if‘) {
                split = ‘ if(‘ + split.join(‘ ‘) + ‘)‘;
            } else {
                split = ‘‘;
            }

            code = ‘}else‘ + split + ‘{‘;
            break;

        case ‘/if‘:

            code = ‘}‘;
            break;

        case ‘each‘:
            
            var object = split[0] || ‘$data‘;
            var as     = split[1] || ‘as‘;
            var value  = split[2] || ‘$value‘;
            var index  = split[3] || ‘$index‘;
            
            var param   = value + ‘,‘ + index;
            
            if (as !== ‘as‘) {
                object = ‘[]‘;
            }
            
            code =  ‘$each(‘ + object + ‘,function(‘ + param + ‘){‘;
            break;

        case ‘/each‘:

            code = ‘});‘;
            break;

        case ‘echo‘:

            code = ‘print(‘ + args + ‘);‘;
            break;

        case ‘print‘:
        case ‘include‘:

            code = key + ‘(‘ + split.join(‘,‘) + ‘);‘;
            break;

        default:

            // 过滤器(辅助方法)
            // {{value | filterA:‘abcd‘ | filterB}}
            // >>> $helpers.filterB($helpers.filterA(value, ‘abcd‘))
            // TODO: {{ddd||aaa}} 不包含空格
            if (/^\s*\|\s*[\w\$]/.test(args)) {

                var escape = true;

                // {{#value | link}}
                if (code.indexOf(‘#‘) === 0) {
                    code = code.substr(1);
                    escape = false;
                }

                var i = 0;
                var array = code.split(‘|‘);
                var len = array.length;
                var val = array[i++];

                for (; i < len; i ++) {
                    val = filtered(val, array[i]);
                }

                code = (escape ? ‘=‘ : ‘=#‘) + val;

            // 即将弃用 {{helperName value}}
            } else if (template.helpers[key]) {
                
                code = ‘=#‘ + key + ‘(‘ + split.join(‘,‘) + ‘);‘;
            
            // 内容直接输出 {{value}}
            } else {

                code = ‘=‘ + code;
            }

            break;
    }
    
    
    return code;
};

 

artTemplate模板引擎

标签:

原文地址:http://www.cnblogs.com/zzcit/p/5694861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!