3
3
// This file provides extension points and other hooks into the twig functionality.
4
4
5
5
module . exports = function ( Twig ) {
6
- " use strict" ;
6
+ ' use strict' ;
7
7
Twig . exports = {
8
8
VERSION : Twig . VERSION
9
9
} ;
@@ -15,17 +15,17 @@ module.exports = function (Twig) {
15
15
*
16
16
* @return {Twig.Template } A Twig template ready for rendering.
17
17
*/
18
- Twig . exports . twig = function twig ( params ) {
18
+ Twig . exports . twig = function ( params ) {
19
19
'use strict' ;
20
- var id = params . id ,
21
- options = {
22
- strict_variables : params . strict_variables || false ,
23
- // TODO: turn autoscape on in the next major version
24
- autoescape : params . autoescape != null && params . autoescape || false ,
25
- allowInlineIncludes : params . allowInlineIncludes || false ,
26
- rethrow : params . rethrow || false ,
27
- namespaces : params . namespaces
28
- } ;
20
+ const { id } = params ;
21
+ const options = {
22
+ strictVariables : params . strictVariables || false ,
23
+ // TODO: turn autoscape on in the next major version
24
+ autoescape : ( params . autoescape !== null && params . autoescape ) || false ,
25
+ allowInlineIncludes : params . allowInlineIncludes || false ,
26
+ rethrow : params . rethrow || false ,
27
+ namespaces : params . namespaces
28
+ } ;
29
29
30
30
if ( Twig . cache && id ) {
31
31
Twig . validateId ( id ) ;
@@ -34,96 +34,101 @@ module.exports = function (Twig) {
34
34
if ( params . debug !== undefined ) {
35
35
Twig . debug = params . debug ;
36
36
}
37
+
37
38
if ( params . trace !== undefined ) {
38
39
Twig . trace = params . trace ;
39
40
}
40
41
41
42
if ( params . data !== undefined ) {
42
43
return Twig . Templates . parsers . twig ( {
43
44
data : params . data ,
44
- path : params . hasOwnProperty ( 'path' ) ? params . path : undefined ,
45
+ path : Object . hasOwnProperty . call ( params , 'path' ) ? params . path : undefined ,
45
46
module : params . module ,
46
- id : id ,
47
- options : options
47
+ id,
48
+ options
48
49
} ) ;
50
+ }
49
51
50
- } else if ( params . ref !== undefined ) {
52
+ if ( params . ref !== undefined ) {
51
53
if ( params . id !== undefined ) {
52
- throw new Twig . Error ( " Both ref and id cannot be set on a twig.js template." ) ;
54
+ throw new Twig . Error ( ' Both ref and id cannot be set on a twig.js template.' ) ;
53
55
}
56
+
54
57
return Twig . Templates . load ( params . ref ) ;
58
+ }
55
59
56
- } else if ( params . method !== undefined ) {
60
+ if ( params . method !== undefined ) {
57
61
if ( ! Twig . Templates . isRegisteredLoader ( params . method ) ) {
58
62
throw new Twig . Error ( 'Loader for "' + params . method + '" is not defined.' ) ;
59
63
}
64
+
60
65
return Twig . Templates . loadRemote
1E0A
( params . name || params . href || params . path || id || undefined , {
61
- id : id ,
66
+ id,
62
67
method : params . method ,
63
68
parser : params . parser || 'twig' ,
64
69
base : params . base ,
65
70
module : params . module ,
66
71
precompiled : params . precompiled ,
67
72
async : params . async ,
68
- options : options
73
+ options
69
74
70
75
} , params . load , params . error ) ;
76
+ }
71
77
72
- } else if ( params . href !== undefined ) {
78
+ if ( params . href !== undefined ) {
73
79
return Twig . Templates . loadRemote ( params . href , {
74
- id : id ,
80
+ id,
75
81
method : 'ajax' ,
76
82
parser : params . parser || 'twig' ,
77
83
base : params . base ,
78
84
module : params . module ,
79
85
precompiled : params . precompiled ,
80
86
async : params . async ,
81
- options : options
87
+ options
82
88
83
89
} , params . load , params . error ) ;
90
+ }
84
91
85
- } else if ( params . path !== undefined ) {
92
+ if ( params . path !== undefined ) {
86
93
return Twig . Templates . loadRemote ( params . path , {
87
- id : id ,
94
+ id,
88
95
method : 'fs' ,
89
96
parser : params . parser || 'twig' ,
90
97
base : params . base ,
91
98
module : params . module ,
92
99
precompiled : params . precompiled ,
93
100
async : params . async ,
94
- options : options
95
-
101
+ options
96
102
} , params . load , params . error ) ;
97
103
}
98
104
} ;
99
105
100
106
// Extend Twig with a new filter.
101
- Twig . exports . extendFilter = function ( filter , definition ) {
107
+ Twig . exports . extendFilter = function ( filter , definition ) {
102
108
Twig . filter . extend ( filter , definition ) ;
103
109
} ;
104
110
105
111
// Extend Twig with a new function.
106
- Twig . exports . extendFunction = function ( fn , definition ) {
112
+ Twig . exports . extendFunction = function ( fn , definition ) {
107
113
Twig . _function . extend ( fn , definition ) ;
108
114
} ;
109
115
110
116
// Extend Twig with a new test.
111
- Twig . exports . extendTest = function ( test , definition ) {
117
+ Twig . exports . extendTest = function ( test , definition ) {
112
118
Twig . test . extend ( test , definition ) ;
113
119
} ;
114
120
115
121
// Extend Twig with a new definition.
116
- Twig . exports . extendTag = function ( definition ) {
122
+ Twig . exports . extendTag = function ( definition ) {
117
123
Twig . logic . extend ( definition ) ;
118
124
} ;
119
125
120
126
// Provide an environment for extending Twig core.
121
127
// Calls fn with the internal Twig object.
122
- Twig . exports . extend = function ( fn ) {
128
+ Twig . exports . extend = function ( fn ) {
123
129
fn ( Twig ) ;
124
130
} ;
125
131
126
-
127
132
/**
128
133
* Provide an extension for use with express 2.
129
134
*
@@ -132,20 +137,19 @@ module.exports = function (Twig) {
132
137
*
133
138
* @return {string } The rendered template.
134
139
*/
135
- Twig . exports . compile = function ( markup , options ) {
136
- var id = options . filename ,
137
- path = options . filename ,
138
- template ;
140
+ Twig . exports . compile = function ( markup , options ) {
141
+ const id = options . filename ;
142
+ const path = options . filename ;
139
143
140
144
// Try to load the template from the cache
141
- template = new Twig . Template ( {
145
+ const template = new Twig . Template ( {
142
146
data : markup ,
143
- path : path ,
144
- id : id ,
147
+ path,
148
+ id,
145
149
options : options . settings [ 'twig options' ]
146
150
} ) ; // Twig.Templates.load(id) ||
147
151
148
- return function ( context ) {
152
+ return function ( context ) {
149
153
retur
A851
n template . render ( context ) ;
150
154
} ;
151
155
} ;
@@ -159,39 +163,39 @@ module.exports = function (Twig) {
159
163
*
160
164
* @throws Twig.Error
161
165
*/
162
- Twig . exports . renderFile = function ( path , options , fn ) {
163
- // handle callback in options
166
+ Twig . exports . renderFile = function ( path , options , fn ) {
167
+ // Handle callback in options
164
168
if ( typeof options === 'function' ) {
165
169
fn = options ;
166
170
options = { } ;
167
171
}
168
172
169
173
options = options || { } ;
170
174
171
- var settings = options . settings || { } ;
175
+ const settings = options . settings || { } ;
172
176
173
- // mixin any options provided to the express app.
174
- var view_options = settings [ 'twig options' ] ;
177
+ // Mixin any options provided to the express app.
178
+ const viewOptions = settings [ 'twig options' ] ;
175
179
176
- var params = {
177
- path : path ,
180
+ const params = {
181
+ path,
178
182
base : settings . views ,
179
- load : function ( template ) {
180
- // render and return template as a simple string, see https://github.com/twigjs/twig.js/pull/348 for more information
181
- if ( ! view_options || ! view_options . allow_async ) {
182
- fn ( null , '' + template . render ( options ) ) ;
183
+ load ( template ) {
184
+ // Render and return template as a simple string, see https://github.com/twigjs/twig.js/pull/348 for more information
185
+ if ( ! viewOptions || ! viewOptions . allow_async ) {
186
+ fn ( null , String ( template . render ( options ) ) ) ;
183
187
return ;
184
188
}
185
189
186
190
template . renderAsync ( options )
187
- . then ( function ( out ) { fn ( null , out ) ; } , fn ) ;
191
+ . then ( out => fn ( null , out ) , fn ) ;
188
192
}
189
193
} ;
190
194
191
- if ( view_options ) {
192
- for ( var option in view_options ) {
193
- if ( view_options . hasOwnProperty ( option ) ) {
194
- params [ option ] = view_options [ option ] ;
195
+ if ( viewOptions ) {
196
+ for ( const option in viewOptions ) {
197
+ if ( Object . hasOwnProperty . call ( viewOptions , option ) ) {
198
+ params [ option ] = viewOptions [ option ] ;
195
199
}
196
200
}
197
201
}
@@ -209,15 +213,15 @@ module.exports = function (Twig) {
209
213
*
210
214
* @param {boolean } cache
211
215
*/
212
- Twig . exports . cache = function ( cache ) {
216
+ Twig . exports . cache = function ( cache ) {
213
217
Twig . cache = cache ;
214
218
} ;
215
219
216
- //We need to export the path module so we can effectively test it
220
+ // We need to export the path module so we can effectively test it
217
221
Twig . exports . path = Twig . path ;
218
222
219
- //Export our filters.
220
- //Resolves #307
223
+ // Export our filters.
224
+ // Resolves #307
221
225
Twig . exports . filters = Twig . filters ;
222
226
223
227
Twig . exports . Promise = Twig . Promise ;
0 commit comments