# Code coverage report for thinkjs/lib/Lib/Core/View.js

## Statements: 100% (35 / 35) Branches: 100% (26 / 26) Functions: 100% (15 / 15) Lines: 100% (35 / 35) Ignored: none

All files » thinkjs/lib/Lib/Core/ » View.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98

    2

    2

    46
    46

    101
    2

    99
    5

    94
    2

    92

    4
    4
    4

    3

    3

    3

    1
    1

    4

    6
    1
    1
    1

    6
    1

    6

    13
    13
    13
    12
    12

    13
    11

    11

     /**
     * view
     * @return {[type]} [description]
     */
    module.exports = Class(function(){
      'use strict';
      return {
        init: function(http){
          this.http = http;
          this.tVar = {};
        },
        /**
         * 给变量赋值
         * @param  {[type]} name  [description]
         * @param  {[type]} value [description]
         * @return {[type]}       [description]
         */
        assign: function(name, value){
          if (name === undefined) {
            return this.tVar;
          }
          if (isString(name) && arguments.length === 1) {
            return this.tVar[name];
          }
          if (isObject(name)) {
            this.tVar = extend(this.tVar, name);
          }else{
            this.tVar[name] = value;
          }
        },
        /**
         * 输出模版文件内容
         * @param  {[type]} templateFile [description]
         * @param  {[type]} charset      [description]
         * @param  {[type]} contentType  [description]
         * @param  {[type]} content      [description]
         * @return {[type]}              [description]
         */
        display: function(templateFile, charset, contentType){
          var self = this;
          return tag('view_init', this.http).then(function(){
            return self.fetch(templateFile);
          }).then(function(content){
            return self.render(content, charset, contentType);
          }).then(function(content){
            return tag('view_end', self.http, content);
          }).then(function(){
            return self.http.end();
          }).catch(function(err){
            console.error(err.stack);
            return self.http.end();
          }).then(function(){
            return getDefer().promise;
          })
        },
        /**
         * 渲染模版
         * @param  {[type]} content     [description]
         * @param  {[type]} charset     [description]
         * @param  {[type]} contentType [description]
         * @return {[type]}             [description]
         */
        render: function(content, charset, contentType){
          if (!this.http.cthIsSend) {
            charset = charset || C('encoding');
            contentType = contentType || C('tpl_content_type');
            this.http.setHeader('Content-Type', contentType + '; charset=' + charset);
          }
          if (C('show_exec_time')) {
            this.http.sendTime('Exec-Time');
          }
          return this.http.echo(content || '', charset || C('encoding'));
        },
        /**
         * 获取模版文件内容
         * @param  {[type]} templateFile [description]
         * @param  {[type]} content      [description]
         * @return {[type]}              [description]
         */
        fetch: function(templateFile){
          var self = this;
          var promise = getPromise(templateFile);
          if (!templateFile || !isFile(templateFile)) {
            promise = tag('view_template', this.http, templateFile).then(function(file){
              return isFile(file) ? file : getPromise(new Error("can't find template file"), true);
            });
          }
          return promise.then(function(templateFile){
            return tag('view_parse', self.http, {
              'var': self.tVar,
              'file': templateFile
            });
          }).then(function(content){
            return tag('view_filter', self.http, content);
          });
        }
      };
    });