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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! Mage is an intuitive and powerful template engine.
//!
//! Supported keywords: `for` `if` `include` `set`
//!
//! Mage support expression evaluate with crate [`eval`](https://crates.io/crates/eval).
//! Built-in functions and operators documentation: [`eval`](https://docs.rs/eval).
//!
//! # Examples
//! Render template from string:
//!
//! ```
//! use mage::Template;
//!
//! let source = "{{if true}}Hello world!{{if}}";
//! let output = Template::new().template(source).render().unwrap();
//! assert_eq!(output, "Hello world!");
//! ```
//!
//! Render template from file:
//!
//! ```
//! use mage::Template;
//!
//! let output = Template::new()
//!     .root("examples")
//!     .extension("html")
//!     .open("main")
//!     .unwrap()
//!     .render()
//!     .unwrap();
//! ```
//!
//!
//! # Keywords
//!
//! ## For
//! Loop a value.
//!
//! ```html
//! {{for value in object}}
//!     {{value}}
//! {{for}}
//!
//! {{for value, key in object}}
//!     {{key}}
//!     {{value}}
//! {{for}}
//! ```
//!
//! ## If
//! Conditionally render a block.
//!
//! ```html
//! {{if boolean}}
//!     Hello world!
//! {{elseif boolean}}
//!     Hello world!
//! {{elseif boolean}}
//!     Hello world!
//! {{else}}
//!     Hello world!
//! {{if}}
//! ```
//!
//! ## Include
//! Include a sub template.
//!
//! ```html
//! {{include views/header}}
//! ```
//!
//! ## Set
//! Set a value to current context.
//!
//! ```html
//! {{set boolean=true}}
//! {{set number=123}}
//! {{set string='Hello world!'}}
//! ```
//!
//!
#![cfg_attr(all(feature = "unstable", test), feature(test))]
#![deny(missing_docs)]

#[macro_use]
extern crate quick_error;
extern crate serde;
extern crate serde_json;
extern crate eval;
extern crate glob;

mod template;
mod render;
mod utils;
mod error;
mod node;
mod tree;
mod keyword;
mod cache;

pub use cache::Cache;
pub use template::RenderOptions;
pub use template::Template;
pub use error::Error;
pub use eval::Error as ExpressionError;
pub use eval::{Context, Contexts, Value, Function, Functions, to_value};

type Compiled = Box<Fn(Contexts, &Functions) -> Result<String, Error>>;



#[cfg(test)]
mod tests {
    use Template;
    use std::collections::HashMap;

    #[test]
    fn test_if() {
        let source = "{{if true}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_if_value() {
        let source = "{{if boolean}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .value("boolean", true)
                       .render()
                       .unwrap(),
                   "Hello world!");
        assert!(Template::new()
            .template(source)
            .value("boolean", false)
            .render()
            .unwrap()
            .is_empty());
    }

    #[test]
    fn test_if_expression() {
        let source = "{{if 1 < 2}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_if_complex_expression() {
        let source = "{{if (2 + 3) * 5 == 25}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_if_is_empty() {
        let source = "{{if is_empty('')}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_if_is_not_empty() {
        let source = "{{if !is_empty('Not Empty')}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_else_if() {
        let source = "{{if false}}{{elseif true}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_else() {
        let source = "{{if false}}{{else}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_set_boolean() {
        let source = "{{set boolean=true}}{{if boolean}}Hello world!{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_set_string() {
        let source = "{{set value='Hello world!'}}{{value}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_set_number() {
        let source = "{{set number=123}}{{number == 123}}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "true");
    }

    #[test]
    fn test_for() {
        let source = "{{for value, index in array}}[{{index}}]{{value}}{{for}}";
        assert_eq!(Template::new()
                       .template(source)
                       .value("array", vec!["Hello", " ", "world", "!"])
                       .render()
                       .unwrap(),
                   "[0]Hello[1] [2]world[3]!");
    }

    #[test]
    fn test_example() {
        Template::new()
            .root("examples")
            .extension("html")
            .open("main")
            .unwrap()
            .render()
            .unwrap();
    }

    #[test]
    fn test_string() {
        let source = "Hello world!";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "Hello world!");
    }

    #[test]
    fn test_expression() {
        let source = "{{  3 + 5 * 4 + 3 - 5 }}";
        assert_eq!(Template::new()
                       .template(source)
                       .render()
                       .unwrap(),
                   "21");
    }

    #[test]
    fn test_object_access() {
        let mut object = HashMap::new();
        object.insert("foo", "Foo, hello world!");
        object.insert("bar", "Bar, hello world!");
        let source = "{{if object.foo == 'Foo, hello world!'}}{{object.bar}}{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .value("object", object)
                       .render()
                       .unwrap(),
                   "Bar, hello world!");
    }

    #[test]
    fn test_array_access() {
        let array = vec!["hello", "world", "!"];
        let source = "{{if array[0] == 'hello'}}{{array[1]}}{{array[2]}}{{if}}";
        assert_eq!(Template::new()
                       .template(source)
                       .value("array", array)
                       .render()
                       .unwrap(),
                   "world!");
    }
}

#[cfg(all(feature = "unstable", test))]
mod benches {
    extern crate test;
    use {Value, Template, to_value};
    use std::collections::HashMap;

    static SOURCE: &'static str = r#"<html>
<head>
    <title>{{year}}</title>
</head>
<body>
    <h1>{{year}}</h1>
    <ul>
        {{for item, index in items}}
        <li class="{{if index == 0}}active{{if}}"> <b>{{item.name}}</b>
            : {{item.score}}
        </li>
        {{for}}
    </ul>
</body>
</html>"#;

    fn create_items() -> Vec<HashMap<String, Value>> {
        let mut items = Vec::new();

        for tuple in &vec![("Abc", 100), ("Def", 105), ("Ghi", 150), ("Jkl", 510)] {
            let (name, score) = *tuple;
            let mut item = HashMap::new();
            item.insert("name".to_owned(), to_value(name));
            item.insert("score".to_owned(), to_value(score));
            items.push(item);
        }

        items
    }

    #[bench]
    fn bench_compile(b: &mut test::Bencher) {
        b.iter(|| {
            Template::new()
                .template(SOURCE)
                .compile()
                .unwrap();
        })
    }

    #[bench]
    fn bench_render(b: &mut test::Bencher) {
        let template = Template::new()
            .template(SOURCE)
            .value("year", 2016)
            .value("items", create_items())
            .compile()
            .unwrap();

        b.iter(|| template.render().unwrap())
    }

    #[bench]
    fn bench_render_without_compile(b: &mut test::Bencher) {
        let items = create_items();

        b.iter(|| {
            Template::new()
                .template(SOURCE)
                .value("year", 2016)
                .value("items", items.clone())
                .render()
                .unwrap();
        })
    }
}