think-template-art

2.0.0 • Public • Published

think-template-art for ThinkJS

Install

  npm install think-template-art

How to use

注册 adapter

添加文件src/common/bootstrap/adapter.js.

import artTemplate from 'think-template-art';
think.adapter('template', 'art', artTemplate);

配置

修改配置文件 src/common/config/view.js:

export default {
  type: 'art',
  content_type: 'text/html',
  file_ext: '.html',
  file_depr: '_',
  root_path: think.ROOT_PATH + '/view',
  helper_path: think.RESOURCE_PATH+'/static/js/template.helper.js',
  adapter: {
    ejs: {},
    art: {
        //这里书写 art-template 配置,如果基础配置存在,将覆盖基础配置
    }
  }
};

helper函数配置

修改 config/view中的 helper_path helper函数的格式为

!(function(){
 
    let helpers  = {};
 
    /**
     * 对日期进行格式化,
     * @param date 要格式化的日期
     * @param format 进行格式化的模式字符串
     *     支持的模式字母有:
     *     y:年,
     *     M:年中的月份(1-12),
     *     d:月份中的天(1-31),
     *     h:小时(0-23),
     *     m:分(0-59),
     *     s:秒(0-59),
     *     S:毫秒(0-999),
     *     q:季度(1-4)
     * @return String
     */
    helpers.dateFormat = function (date, format) {
        if( [].toString.call(date) != '[object Date]' ){
            date = new Date(date-0);
        }
        var map = {
            "M": date.getMonth() + 1, //月份
            "d": date.getDate(), //
            "h": date.getHours(), //小时
            "m": date.getMinutes(), //
            "s": date.getSeconds(), //
            "q": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
 
        console.log(map);
        format = format.replace(/([yMdhmsqS])+/g, function(all, t){
            var v = map[t];
            if(!== undefined){
                if(all.length > 1){
                    v = '0' + v;
                    v = v.substr(v.length-2);
                }
                return v;
            }
            else if(=== 'y'){
                return (date.getFullYear() + '').substr(4 - all.length);
            }
            return all;
        });
        return format;
    };
 
    // RequireJS && SeaJS
    if (typeof define === 'function') {
        define(function() {
            return helpers;
        });
    // NodeJS
    } else if (typeof exports !== 'undefined') {
        module.exports = helpers;
    } else {
        this.helpers = helpers;
    }
})();

拷贝后放到你需要的位置

如果你需要同构页面务必把文件放到 /www/static

辅助方法的例子 | art-template文档 | ThinkJS文档

LICENSE

MIT

Package Sidebar

Install

npm i think-template-art

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • liweifeng