-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
index.js
138 lines (126 loc) · 3.14 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const nunjucks = require('nunjucks')
const ViewLoader = require('./loader')
const viewFilters = require('./filters')
const viewGlobals = require('./globals')
const _ = require('lodash')
/**
* View class for adonis framework to serve jinja like views
* @class
* @alias View
*/
class View {
constructor (Helpers, Config, Route, viewsEnv) {
this.Helpers = Helpers
this.Config = Config
this.Route = Route
nunjucks.nodes.For = nunjucks.nodes.AsyncEach // monkey patch for with asyncEach
const viewsPath = Helpers.viewsPath()
const viewsCache = Config.get('app.views.cache', true)
const injectServices = Config.get('app.views.injectServices', false)
this.viewsEnv = viewsEnv || new nunjucks.Environment(new ViewLoader(viewsPath, false, !viewsCache))
/**
* only register use, make and yield when the end user
* has enabled injectServices inside the config file.
*/
if (injectServices) {
require('./services')(this.viewsEnv)
}
viewGlobals(this.viewsEnv, Route)
viewFilters(this.viewsEnv, Route)
}
/**
* compile a view with give template and data
*
* @param {String} template_path
* @param {Object} [data]
* @return {Promise}
*
* @example
* View
* .make('index', {})
* .then()
* .catch()
* @public
*/
make (templatePath, data) {
let self = this
return new Promise(function (resolve, reject) {
self.viewsEnv.render(templatePath, data, function (err, templateContent) {
if (err) {
reject(err)
return
}
resolve(templateContent)
})
})
}
/**
* makes a view from string instead of path, it is
* helpful for making quick templates on the
* fly.
*
* @param {String} templateString
* @param {Object} [data]
* @return {String}
*
* @example
* view.makeString('Hello {{ user }}', {user: 'doe'})
*
* @public
*/
makeString (templateString, data) {
return this.viewsEnv.renderString(templateString, data)
}
/**
* add a filter to view, it also support async execution
*
* @param {String} name
* @param {Function} callback
* @param {Boolean} async
*
* @example
* View.filter('name', function () {
* }, true)
*
* @public
*/
filter (name, callback, async) {
this.viewsEnv.addFilter(name, callback, async)
}
/**
* add a global method to views
*
* @param {String} name
* @param {Mixed} value
*
* @example
* View.global('key', value)
*
* @public
*/
global (name, value) {
this.viewsEnv.addGlobal(name, value)
}
/**
* Returns a cloned instance of itself to be used for
* having isoloted instance of views. This is required
* to attach globals during request lifecycle.
*
* @method clone
*
* @return {Object}
*/
clone () {
return new View(this.Helpers, this.Config, this.Route, _.cloneDeep(this.viewsEnv))
}
}
module.exports = View