forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
61 lines (56 loc) · 1.25 KB
/
loader.js
File metadata and controls
61 lines (56 loc) · 1.25 KB
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
'use strict'
/**
* adonis-framework
* Copyright(c) 2015-2016 Harminder Virk
* MIT Licensed
*/
const nunjucks = require('nunjucks')
const path = require('path')
const fs = require('fs')
/**
* Views loader
* @module
* @alias Views.Loader
*/
exports = module.exports = nunjucks.Loader.extend({
/**
* Initiates views loader
*
* @param {String} viewsPath
* @param {Boolean} noWatch Not considered
* @param {Boolean} noCache
*
* @public
*/
init: function (viewsPath, noWatch, noCache) {
this.viewsPath = path.normalize(viewsPath)
this.async = true
this.noCache = !!noCache
},
/**
* get content of a file required while rendering
* template
*
* @param {String} name
* @param {Function} callback
*
* @public
*/
getSource: function (name, callback) {
name = name.replace(/((?!\.+\/)\.(?!njk))/g, '/')
name = path.extname(name) === '.njk' ? name : `${name}.njk`
const viewPath = path.resolve(this.viewsPath, name)
const self = this
fs.readFile(viewPath, function (err, content) {
if (err) {
callback(null, null)
return
}
callback(null, {
src: content.toString(),
path: viewPath,
noCache: self.noCache
})
})
}
})