• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sunng87 / handlebars-rust / 20657583393

02 Jan 2026 12:10PM UTC coverage: 83.449% (-0.3%) from 83.707%
20657583393

Pull #733

github

web-flow
Merge 2226d6e72 into 718db6bb1
Pull Request #733: fix: block scoped inline

26 of 27 new or added lines in 4 files covered. (96.3%)

10 existing lines in 2 files now uncovered.

1679 of 2012 relevant lines covered (83.45%)

7.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.81
/src/render.rs
1
use std::borrow::Cow;
2
use std::collections::{BTreeMap, VecDeque};
3
use std::fmt;
4
use std::rc::Rc;
5

6
use serde_json::value::Value as Json;
7

8
use crate::block::BlockContext;
9
use crate::context::Context;
10
use crate::error::RenderError;
11
use crate::helpers::HelperDef;
12
use crate::json::path::Path;
13
use crate::json::value::{JsonRender, PathAndJson, ScopedJson};
14
use crate::output::{Output, StringOutput};
15
use crate::registry::Registry;
16
use crate::support;
17
use crate::support::str::newline_matcher;
18
use crate::template::TemplateElement::{
19
    DecoratorBlock, DecoratorExpression, Expression, HelperBlock, HtmlExpression, PartialBlock,
20
    PartialExpression, RawString,
21
};
22
use crate::template::{
23
    BlockParam, DecoratorTemplate, HelperTemplate, Parameter, Template, TemplateElement,
24
    TemplateMapping,
25
};
26
use crate::{partial, RenderErrorReason};
27

28
const HELPER_MISSING: &str = "helperMissing";
29
const BLOCK_HELPER_MISSING: &str = "blockHelperMissing";
30

31
/// The context of a render call
32
///
33
/// This context stores information of a render and a writer where generated
34
/// content is written to.
35
///
36
#[derive(Clone)]
37
pub struct RenderContext<'reg: 'rc, 'rc> {
38
    dev_mode_templates: Option<&'rc BTreeMap<String, Cow<'rc, Template>>>,
39

40
    blocks: VecDeque<BlockContext<'rc>>,
41

42
    // copy-on-write context
43
    modified_context: Option<Rc<Context>>,
44

45
    // when rendering partials, store rendered string into partial_block_stack
46
    // for `@partial-block` referencing. If it's a `{{> partial}}`, push `None`
47
    // to stack and report error when child partial try to referencing
48
    // `@partial-block`.
49
    partial_block_stack: VecDeque<Option<String>>,
50
    local_helpers: BTreeMap<String, Rc<dyn HelperDef + Send + Sync + 'rc>>,
51
    /// current template name
52
    current_template: Option<&'rc String>,
53
    /// root template name
54
    root_template: Option<&'reg String>,
55
    disable_escape: bool,
56

57
    // Indicates whether the previous text that we rendered ended on a newline.
58
    // This is necessary to make indenting decisions after the end of partials.
59
    trailing_newline: bool,
60

61
    // This should be set to true whenever any output is written.
62
    // We need this to detect empty partials/helpers for indenting decisions.
63
    content_produced: bool,
64

65
    // The next text that we render should indent itself.
66
    indent_before_write: bool,
67
    indent_string: Option<Cow<'rc, str>>,
68

69
    // Threads the recursive_lookup state from the registry down
70
    // through to the context navigation layer
71
    recursive_lookup: bool,
72
}
73

74
impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
75
    /// Create a render context
76
    pub fn new(root_template: Option<&'reg String>) -> RenderContext<'reg, 'rc> {
16✔
77
        let mut blocks = VecDeque::with_capacity(5);
17✔
78
        blocks.push_front(BlockContext::new());
32✔
79

80
        let modified_context = None;
15✔
81
        RenderContext {
82
            partial_block_stack: VecDeque::new(),
18✔
83
            local_helpers: BTreeMap::new(),
15✔
84
            current_template: None,
85
            root_template,
86
            disable_escape: false,
87
            trailing_newline: false,
88
            content_produced: false,
89
            indent_before_write: false,
90
            indent_string: None,
91
            blocks,
92
            modified_context,
93
            dev_mode_templates: None,
94
            recursive_lookup: false,
95
        }
96
    }
97

98
    /// Push a block context into render context stack. This is typically
99
    /// called when you entering a block scope.
100
    pub fn push_block(&mut self, block: BlockContext<'rc>) {
12✔
101
        self.blocks.push_front(block);
14✔
102
    }
103

104
    /// Pop and drop current block context.
105
    /// This is typically called when leaving a block scope.
106
    pub fn pop_block(&mut self) {
9✔
107
        self.blocks.pop_front();
10✔
108
    }
109

110
    /// Remove all blocks
111
    pub(crate) fn clear_blocks(&mut self) {
×
112
        self.blocks.clear();
×
113
    }
114

115
    /// Borrow a reference to current block context
116
    pub fn block(&self) -> Option<&BlockContext<'rc>> {
×
117
        self.blocks.front()
×
118
    }
119

120
    /// Borrow a mutable reference to current block context in order to
121
    /// modify some data.
122
    pub fn block_mut(&mut self) -> Option<&mut BlockContext<'rc>> {
8✔
123
        self.blocks.front_mut()
7✔
124
    }
125

126
    /// Replace blocks hold by render context and return the replaced blocks
UNCOV
127
    pub fn replace_blocks(
×
128
        &mut self,
129
        blocks: VecDeque<BlockContext<'rc>>,
130
    ) -> VecDeque<BlockContext<'rc>> {
UNCOV
131
        std::mem::replace(&mut self.blocks, blocks)
×
132
    }
133

134
    /// Get the modified context data if any
135
    pub fn context(&self) -> Option<Rc<Context>> {
13✔
136
        self.modified_context.clone()
14✔
137
    }
138

139
    /// Set new context data into the render process.
140
    /// This is typically called in decorators where user can modify
141
    /// the data they were rendering.
142
    pub fn set_context(&mut self, ctx: Context) {
1✔
143
        self.modified_context = Some(Rc::new(ctx));
2✔
144
    }
145

146
    /// Evaluate a Json path in current scope.
147
    ///
148
    /// Typically you don't need to evaluate it by yourself.
149
    /// The Helper and Decorator API will provide your evaluated value of
150
    /// their parameters and hash data.
151
    pub fn evaluate(
3✔
152
        &self,
153
        context: &'rc Context,
154
        relative_path: &str,
155
    ) -> Result<ScopedJson<'rc>, RenderError> {
156
        let path = Path::parse(relative_path)?;
3✔
157
        self.evaluate2(context, &path)
3✔
158
    }
159

160
    pub(crate) fn evaluate2(
14✔
161
        &self,
162
        context: &'rc Context,
163
        path: &Path,
164
    ) -> Result<ScopedJson<'rc>, RenderError> {
165
        match path {
14✔
166
            Path::Local((level, name, _)) => Ok(self
2✔
167
                .get_local_var(*level, name)
1✔
168
                .map_or_else(|| ScopedJson::Missing, |v| ScopedJson::Derived(v.clone()))),
6✔
169
            Path::Relative((segs, _)) => {
14✔
170
                context.navigate(segs, &self.blocks, self.recursive_lookup)
14✔
171
            }
172
        }
173
    }
174

175
    /// Get registered partial in this render context
176
    pub fn get_partial(&self, name: &str) -> Option<&'rc Template> {
6✔
177
        // resolve partial from block
178
        for block in &self.blocks {
13✔
179
            if let Some(partial) = block.get_local_partial(name) {
11✔
180
                return Some(partial);
5✔
181
            }
182
        }
183

184
        None
4✔
185
    }
186

187
    pub(crate) fn push_partial_block(&mut self, partial_block: Option<String>) {
8✔
188
        self.partial_block_stack.push_front(partial_block);
8✔
189
    }
190

191
    pub(crate) fn pop_partial_block(&mut self) {
6✔
192
        self.partial_block_stack.pop_front();
7✔
193
    }
194

195
    pub(crate) fn peek_partial_block(&self) -> Option<&Option<String>> {
1✔
196
        self.partial_block_stack.front()
1✔
197
    }
198

199
    pub(crate) fn set_indent_string(&mut self, indent: Option<Cow<'rc, str>>) {
7✔
200
        self.indent_string = indent;
13✔
201
    }
202

203
    #[inline]
204
    pub(crate) fn get_indent_string(&self) -> Option<&Cow<'rc, str>> {
14✔
205
        self.indent_string.as_ref()
15✔
206
    }
207

208
    pub(crate) fn get_dev_mode_template(&self, name: &str) -> Option<&'rc Template> {
4✔
209
        self.dev_mode_templates
4✔
210
            .and_then(|dmt| dmt.get(name).map(|t| &**t))
4✔
211
    }
212

213
    pub(crate) fn set_dev_mode_templates(
1✔
214
        &mut self,
215
        t: Option<&'rc BTreeMap<String, Cow<'rc, Template>>>,
216
    ) {
217
        self.dev_mode_templates = t;
1✔
218
    }
219

220
    fn get_local_var(&self, level: usize, name: &str) -> Option<&Json> {
1✔
221
        self.blocks
×
222
            .get(level)
1✔
223
            .and_then(|blk| blk.get_local_var(name))
3✔
224
    }
225

226
    /// Test if given template name is current template.
227
    pub fn is_current_template(&self, p: &str) -> bool {
8✔
228
        self.current_template.is_some_and(|s| s == p)
12✔
229
    }
230

231
    /// Register a helper in this render context.
232
    /// This is a feature provided by Decorator where you can create
233
    /// temporary helpers.
234
    pub fn register_local_helper(
1✔
235
        &mut self,
236
        name: &str,
237
        def: Box<dyn HelperDef + Send + Sync + 'rc>,
238
    ) {
239
        self.local_helpers.insert(name.to_string(), def.into());
2✔
240
    }
241

242
    /// Remove a helper from render context
243
    pub fn unregister_local_helper(&mut self, name: &str) {
1✔
244
        self.local_helpers.remove(name);
1✔
245
    }
246

247
    /// Attempt to get a helper from current render context.
248
    pub fn get_local_helper(&self, name: &str) -> Option<Rc<dyn HelperDef + Send + Sync + 'rc>> {
10✔
249
        self.local_helpers.get(name).cloned()
11✔
250
    }
251

252
    #[inline]
253
    fn has_local_helper(&self, name: &str) -> bool {
14✔
254
        self.local_helpers.contains_key(name)
14✔
255
    }
256

257
    /// Returns the current template name.
258
    /// Note that the name can be vary from root template when you are rendering
259
    /// from partials.
260
    pub fn get_current_template_name(&self) -> Option<&'rc String> {
8✔
261
        self.current_template
8✔
262
    }
263

264
    /// Set the current template name.
265
    pub fn set_current_template_name(&mut self, name: Option<&'rc String>) {
19✔
266
        self.current_template = name;
16✔
267
    }
268

269
    /// Get root template name if any.
270
    /// This is the template name that you call `render` from `Handlebars`.
271
    pub fn get_root_template_name(&self) -> Option<&'reg String> {
×
272
        self.root_template
×
273
    }
274

275
    /// Get the escape toggle
276
    pub fn is_disable_escape(&self) -> bool {
15✔
277
        self.disable_escape
11✔
278
    }
279

280
    /// Set the escape toggle.
281
    /// When toggle is on, `escape_fn` will be called when rendering.
282
    pub fn set_disable_escape(&mut self, disable: bool) {
4✔
283
        self.disable_escape = disable;
4✔
284
    }
285

286
    #[inline]
287
    pub fn set_trailing_newline(&mut self, trailing_newline: bool) {
16✔
288
        self.trailing_newline = trailing_newline;
15✔
289
    }
290

291
    #[inline]
292
    pub fn get_trailine_newline(&self) -> bool {
16✔
293
        self.trailing_newline
17✔
294
    }
295

296
    #[inline]
297
    pub fn set_content_produced(&mut self, content_produced: bool) {
17✔
298
        self.content_produced = content_produced;
16✔
299
    }
300

301
    #[inline]
302
    pub fn get_content_produced(&self) -> bool {
13✔
303
        self.content_produced
14✔
304
    }
305

306
    #[inline]
307
    pub fn set_indent_before_write(&mut self, indent_before_write: bool) {
16✔
308
        self.indent_before_write = indent_before_write;
17✔
309
    }
310

311
    #[inline]
312
    pub fn get_indent_before_write(&self) -> bool {
18✔
313
        self.indent_before_write
15✔
314
    }
315

316
    pub fn set_recursive_lookup(&mut self, enabled: bool) {
15✔
317
        self.recursive_lookup = enabled;
18✔
318
    }
319
}
320

321
impl fmt::Debug for RenderContext<'_, '_> {
322
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
×
323
        f.debug_struct("RenderContextInner")
×
324
            .field("dev_mode_templates", &self.dev_mode_templates)
×
325
            .field("blocks", &self.blocks)
×
326
            .field("modified_context", &self.modified_context)
×
327
            .field("partial_block_stack", &self.partial_block_stack)
×
328
            .field("root_template", &self.root_template)
×
329
            .field("current_template", &self.current_template)
×
330
            .field("disable_escape", &self.disable_escape)
×
331
            .finish()
332
    }
333
}
334

335
/// Render-time Helper data when using in a helper definition
336
#[derive(Debug, Clone)]
337
pub struct Helper<'rc> {
338
    name: Cow<'rc, str>,
339
    params: Vec<PathAndJson<'rc>>,
340
    hash: BTreeMap<&'rc str, PathAndJson<'rc>>,
341
    template: Option<&'rc Template>,
342
    inverse: Option<&'rc Template>,
343
    block_param: Option<&'rc BlockParam>,
344
    block: bool,
345
}
346

347
impl<'reg: 'rc, 'rc> Helper<'rc> {
348
    fn try_from_template(
11✔
349
        ht: &'rc HelperTemplate,
350
        registry: &'reg Registry<'reg>,
351
        context: &'rc Context,
352
        render_context: &mut RenderContext<'reg, 'rc>,
353
    ) -> Result<Helper<'rc>, RenderError> {
354
        let name = ht.name.expand_as_name(registry, context, render_context)?;
11✔
355
        let mut pv = Vec::with_capacity(ht.params.len());
22✔
356
        for p in &ht.params {
31✔
357
            let r = p.expand(registry, context, render_context)?;
20✔
358
            pv.push(r);
11✔
359
        }
360

361
        let mut hm = BTreeMap::new();
11✔
362
        for (k, p) in &ht.hash {
24✔
363
            let r = p.expand(registry, context, render_context)?;
4✔
364
            hm.insert(k.as_ref(), r);
5✔
365
        }
366

367
        Ok(Helper {
13✔
368
            name,
12✔
369
            params: pv,
12✔
370
            hash: hm,
12✔
371
            template: ht.template.as_ref(),
12✔
372
            inverse: ht.inverse.as_ref(),
12✔
373
            block_param: ht.block_param.as_ref(),
12✔
374
            block: ht.block,
12✔
375
        })
376
    }
377

378
    /// Returns helper name
379
    pub fn name(&self) -> &str {
12✔
380
        &self.name
10✔
381
    }
382

383
    /// Returns all helper params, resolved within the context
384
    pub fn params(&self) -> &Vec<PathAndJson<'rc>> {
3✔
385
        &self.params
×
386
    }
387

388
    /// Returns nth helper param, resolved within the context.
389
    ///
390
    /// ## Example
391
    ///
392
    /// To get the first param in `{{my_helper abc}}` or `{{my_helper 2}}`,
393
    /// use `h.param(0)` in helper definition.
394
    /// Variable `abc` is auto resolved in current context.
395
    ///
396
    /// ```
397
    /// use handlebars::*;
398
    ///
399
    /// fn my_helper(h: &Helper, rc: &mut RenderContext) -> Result<(), RenderError> {
400
    ///     let v = h.param(0).map(|v| v.value())
401
    ///         .ok_or(RenderErrorReason::ParamNotFoundForIndex("myhelper", 0));
402
    ///     // ..
403
    ///     Ok(())
404
    /// }
405
    /// ```
406
    pub fn param(&self, idx: usize) -> Option<&PathAndJson<'rc>> {
11✔
407
        self.params.get(idx)
11✔
408
    }
409

410
    /// Returns hash, resolved within the context
411
    pub fn hash(&self) -> &BTreeMap<&'rc str, PathAndJson<'rc>> {
2✔
412
        &self.hash
2✔
413
    }
414

415
    /// Return hash value of a given key, resolved within the context
416
    ///
417
    /// ## Example
418
    ///
419
    /// To get the first param in `{{my_helper v=abc}}` or `{{my_helper v=2}}`,
420
    /// use `h.hash_get("v")` in helper definition.
421
    /// Variable `abc` is auto resolved in current context.
422
    ///
423
    /// ```
424
    /// use handlebars::*;
425
    ///
426
    /// fn my_helper(h: &Helper, rc: &mut RenderContext) -> Result<(), RenderError> {
427
    ///     let v = h.hash_get("v").map(|v| v.value())
428
    ///         .ok_or(RenderErrorReason::ParamNotFoundForIndex("my_helper", 0));
429
    ///     // ..
430
    ///     Ok(())
431
    /// }
432
    /// ```
433
    pub fn hash_get(&self, key: &str) -> Option<&PathAndJson<'rc>> {
4✔
434
        self.hash.get(key)
4✔
435
    }
436

437
    /// Returns the default inner template if the helper is a block helper.
438
    ///
439
    /// Typically you will render the template via: `template.render(registry, render_context)`
440
    ///
441
    pub fn template(&self) -> Option<&'rc Template> {
8✔
442
        self.template
10✔
443
    }
444

445
    /// Returns the template of `else` branch if any
446
    pub fn inverse(&self) -> Option<&'rc Template> {
2✔
447
        self.inverse
2✔
448
    }
449

450
    /// Returns if the helper is a block one `{{#helper}}{{/helper}}` or not `{{helper 123}}`
451
    pub fn is_block(&self) -> bool {
2✔
452
        self.block
2✔
453
    }
454

455
    /// Returns if the helper has either a block param or block param pair
456
    pub fn has_block_param(&self) -> bool {
×
457
        self.block_param.is_some()
×
458
    }
459

460
    /// Returns block param if any
461
    pub fn block_param(&self) -> Option<&'rc str> {
8✔
462
        if let Some(&BlockParam::Single(Parameter::Name(ref s))) = self.block_param {
16✔
463
            Some(s)
2✔
464
        } else {
465
            None
8✔
466
        }
467
    }
468

469
    /// Return block param pair (for example |key, val|) if any
470
    pub fn block_param_pair(&self) -> Option<(&'rc str, &'rc str)> {
9✔
471
        if let Some(&BlockParam::Pair((Parameter::Name(ref s1), Parameter::Name(ref s2)))) =
17✔
472
            self.block_param
×
473
        {
474
            Some((s1, s2))
2✔
475
        } else {
476
            None
9✔
477
        }
478
    }
479
}
480

481
/// Render-time Decorator data when using in a decorator definition
482
#[derive(Debug)]
483
pub struct Decorator<'rc> {
484
    name: Cow<'rc, str>,
485
    params: Vec<PathAndJson<'rc>>,
486
    hash: BTreeMap<&'rc str, PathAndJson<'rc>>,
487
    template: Option<&'rc Template>,
488
    indent: Option<Cow<'rc, str>>,
489
}
490

491
impl<'reg: 'rc, 'rc> Decorator<'rc> {
492
    fn try_from_template(
7✔
493
        dt: &'rc DecoratorTemplate,
494
        registry: &'reg Registry<'reg>,
495
        context: &'rc Context,
496
        render_context: &mut RenderContext<'reg, 'rc>,
497
    ) -> Result<Decorator<'rc>, RenderError> {
498
        let name = dt.name.expand_as_name(registry, context, render_context)?;
7✔
499

500
        let mut pv = Vec::with_capacity(dt.params.len());
14✔
501
        for p in &dt.params {
19✔
502
            let r = p.expand(registry, context, render_context)?;
10✔
503
            pv.push(r);
5✔
504
        }
505

506
        let mut hm = BTreeMap::new();
6✔
507
        for (k, p) in &dt.hash {
15✔
508
            let r = p.expand(registry, context, render_context)?;
4✔
509
            hm.insert(k.as_ref(), r);
5✔
510
        }
511

512
        let indent = match (render_context.get_indent_string(), dt.indent.as_ref()) {
6✔
513
            (None, None) => None,
6✔
514
            (Some(s), None) => Some(s.clone()),
1✔
515
            (None, Some(s)) => Some(Cow::Borrowed(&**s)),
6✔
516
            (Some(s1), Some(s2)) => {
3✔
517
                let mut res = s1.to_string();
6✔
518
                res.push_str(s2);
6✔
519
                Some(Cow::from(res))
3✔
520
            }
521
        };
522

523
        Ok(Decorator {
6✔
524
            name,
6✔
525
            params: pv,
6✔
526
            hash: hm,
6✔
527
            template: dt.template.as_ref(),
6✔
528
            indent,
6✔
529
        })
530
    }
531

532
    /// Returns helper name
533
    pub fn name(&self) -> &str {
6✔
534
        self.name.as_ref()
6✔
535
    }
536

537
    /// Returns all helper params, resolved within the context
538
    pub fn params(&self) -> &Vec<PathAndJson<'rc>> {
×
539
        &self.params
×
540
    }
541

542
    /// Returns nth helper param, resolved within the context
543
    pub fn param(&self, idx: usize) -> Option<&PathAndJson<'rc>> {
7✔
544
        self.params.get(idx)
6✔
545
    }
546

547
    /// Returns hash, resolved within the context
548
    pub fn hash(&self) -> &BTreeMap<&'rc str, PathAndJson<'rc>> {
7✔
549
        &self.hash
6✔
550
    }
551

552
    /// Return hash value of a given key, resolved within the context
553
    pub fn hash_get(&self, key: &str) -> Option<&PathAndJson<'rc>> {
×
554
        self.hash.get(key)
×
555
    }
556

557
    /// Returns the default inner template if any
558
    pub fn template(&self) -> Option<&'rc Template> {
6✔
559
        self.template
7✔
560
    }
561

562
    pub fn indent(&self) -> Option<&Cow<'rc, str>> {
7✔
563
        self.indent.as_ref()
8✔
564
    }
565
}
566

567
/// Render trait
568
pub trait Renderable {
569
    /// render into `RenderContext`'s `writer`
570
    fn render<'reg: 'rc, 'rc>(
571
        &'rc self,
572
        registry: &'reg Registry<'reg>,
573
        context: &'rc Context,
574
        rc: &mut RenderContext<'reg, 'rc>,
575
        out: &mut dyn Output,
576
    ) -> Result<(), RenderError>;
577

578
    /// render into string
579
    fn renders<'reg: 'rc, 'rc>(
×
580
        &'rc self,
581
        registry: &'reg Registry<'reg>,
582
        ctx: &'rc Context,
583
        rc: &mut RenderContext<'reg, 'rc>,
584
    ) -> Result<String, RenderError> {
585
        let mut so = StringOutput::new();
×
586
        self.render(registry, ctx, rc, &mut so)?;
×
587
        so.into_string()
×
588
            .map_err(|e| RenderErrorReason::from(e).into())
×
589
    }
590
}
591

592
/// Evaluate decorator
593
pub trait Evaluable {
594
    fn eval<'reg: 'rc, 'rc>(
595
        &'rc self,
596
        registry: &'reg Registry<'reg>,
597
        context: &'rc Context,
598
        rc: &mut RenderContext<'reg, 'rc>,
599
    ) -> Result<(), RenderError>;
600
}
601

602
#[inline]
603
fn call_helper_for_value<'reg: 'rc, 'rc>(
4✔
604
    hd: &dyn HelperDef,
605
    ht: &Helper<'rc>,
606
    r: &'reg Registry<'reg>,
607
    ctx: &'rc Context,
608
    rc: &mut RenderContext<'reg, 'rc>,
609
) -> Result<PathAndJson<'rc>, RenderError> {
610
    match hd.call_inner(ht, r, ctx, rc) {
2✔
611
        Ok(result) => Ok(PathAndJson::new(None, result)),
2✔
612
        Err(e) => {
2✔
613
            if e.is_unimplemented() {
6✔
614
                // parse value from output
615
                let mut so = StringOutput::new();
1✔
616

617
                // here we don't want subexpression result escaped,
618
                // so we temporarily disable it
619
                let disable_escape = rc.is_disable_escape();
2✔
620
                rc.set_disable_escape(true);
1✔
621

622
                hd.call(ht, r, ctx, rc, &mut so)?;
1✔
623
                rc.set_disable_escape(disable_escape);
1✔
624

625
                let string = so.into_string().map_err(RenderError::from)?;
1✔
626
                Ok(PathAndJson::new(
2✔
627
                    None,
1✔
628
                    ScopedJson::Derived(Json::String(string)),
1✔
629
                ))
630
            } else {
631
                Err(e)
1✔
632
            }
633
        }
634
    }
635
}
636

637
impl Parameter {
638
    pub fn expand_as_name<'reg: 'rc, 'rc>(
16✔
639
        &'rc self,
640
        registry: &'reg Registry<'reg>,
641
        ctx: &'rc Context,
642
        rc: &mut RenderContext<'reg, 'rc>,
643
    ) -> Result<Cow<'rc, str>, RenderError> {
644
        match self {
15✔
645
            Parameter::Name(ref name) => Ok(Cow::Borrowed(name)),
13✔
646
            Parameter::Path(ref p) => Ok(Cow::Borrowed(p.raw())),
13✔
647
            Parameter::Subexpression(_) => self
×
648
                .expand(registry, ctx, rc)
1✔
649
                .map(|v| v.value().render())
3✔
650
                .map(Cow::Owned),
1✔
651
            Parameter::Literal(ref j) => Ok(Cow::Owned(j.render())),
×
652
        }
653
    }
654

655
    pub fn expand<'reg: 'rc, 'rc>(
15✔
656
        &'rc self,
657
        registry: &'reg Registry<'reg>,
658
        ctx: &'rc Context,
659
        rc: &mut RenderContext<'reg, 'rc>,
660
    ) -> Result<PathAndJson<'rc>, RenderError> {
661
        match self {
14✔
662
            Parameter::Name(ref name) => {
×
663
                // FIXME: raise error when expanding with name?
664
                Ok(PathAndJson::new(Some(name.to_owned()), ScopedJson::Missing))
×
665
            }
666
            Parameter::Path(ref path) => {
12✔
667
                if let Some(rc_context) = rc.context() {
14✔
668
                    let result = rc.evaluate2(&rc_context, path)?;
2✔
669
                    Ok(PathAndJson::new(
2✔
670
                        Some(path.raw().to_owned()),
2✔
671
                        ScopedJson::Derived(result.as_json().clone()),
2✔
672
                    ))
673
                } else {
674
                    let result = rc.evaluate2(ctx, path)?;
25✔
675
                    Ok(PathAndJson::new(Some(path.raw().to_owned()), result))
25✔
676
                }
677
            }
678
            Parameter::Literal(ref j) => Ok(PathAndJson::new(None, ScopedJson::Constant(j))),
8✔
679
            Parameter::Subexpression(ref t) => match *t.as_element() {
2✔
680
                Expression(ref ht) => {
4✔
681
                    let name = ht.name.expand_as_name(registry, ctx, rc)?;
6✔
682

683
                    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
6✔
684
                    if let Some(ref d) = rc.get_local_helper(&name) {
8✔
685
                        call_helper_for_value(d.as_ref(), &h, registry, ctx, rc)
×
686
                    } else {
687
                        let mut helper = registry.get_or_load_helper(&name)?;
6✔
688

689
                        if helper.is_none() {
7✔
690
                            helper = registry.get_or_load_helper(if ht.block {
4✔
691
                                BLOCK_HELPER_MISSING
×
692
                            } else {
693
                                HELPER_MISSING
1✔
694
                            })?;
695
                        }
696

697
                        helper
2✔
698
                            .ok_or_else(|| {
4✔
699
                                RenderErrorReason::HelperNotFound(name.to_string()).into()
×
700
                            })
701
                            .and_then(|d| call_helper_for_value(d.as_ref(), &h, registry, ctx, rc))
8✔
702
                    }
703
                }
704
                _ => unreachable!(),
705
            },
706
        }
707
    }
708
}
709

710
impl Renderable for Template {
711
    fn render<'reg: 'rc, 'rc>(
19✔
712
        &'rc self,
713
        registry: &'reg Registry<'reg>,
714
        ctx: &'rc Context,
715
        rc: &mut RenderContext<'reg, 'rc>,
716
        out: &mut dyn Output,
717
    ) -> Result<(), RenderError> {
718
        rc.set_current_template_name(self.name.as_ref());
16✔
719
        let iter = self.elements.iter();
19✔
720

721
        for (idx, t) in iter.enumerate() {
35✔
722
            t.render(registry, ctx, rc, out).map_err(|mut e| {
20✔
723
                // add line/col number if the template has mapping data
724
                if e.line_no.is_none() {
4✔
725
                    if let Some(&TemplateMapping(line, col)) = self.mapping.get(idx) {
4✔
726
                        e.line_no = Some(line);
2✔
727
                        e.column_no = Some(col);
2✔
728
                    }
729
                }
730

731
                if e.template_name.is_none() {
4✔
732
                    e.template_name.clone_from(&self.name);
2✔
733
                }
734

735
                e
2✔
736
            })?;
737
        }
738

739
        Ok(())
15✔
740
    }
741
}
742

743
impl Evaluable for Template {
UNCOV
744
    fn eval<'reg: 'rc, 'rc>(
×
745
        &'rc self,
746
        registry: &'reg Registry<'reg>,
747
        ctx: &'rc Context,
748
        rc: &mut RenderContext<'reg, 'rc>,
749
    ) -> Result<(), RenderError> {
UNCOV
750
        let iter = self.elements.iter();
×
751

UNCOV
752
        for (idx, t) in iter.enumerate() {
×
UNCOV
753
            t.eval(registry, ctx, rc).map_err(|mut e| {
×
754
                if e.line_no.is_none() {
×
755
                    if let Some(&TemplateMapping(line, col)) = self.mapping.get(idx) {
×
756
                        e.line_no = Some(line);
×
757
                        e.column_no = Some(col);
×
758
                    }
759
                }
760

761
                e.template_name.clone_from(&self.name);
×
762
                e
×
763
            })?;
764
        }
UNCOV
765
        Ok(())
×
766
    }
767
}
768

769
fn helper_exists<'reg: 'rc, 'rc>(
12✔
770
    name: &str,
771
    reg: &Registry<'reg>,
772
    rc: &RenderContext<'reg, 'rc>,
773
) -> bool {
774
    rc.has_local_helper(name) || reg.has_helper(name)
13✔
775
}
776

777
#[inline]
778
fn render_helper<'reg: 'rc, 'rc>(
11✔
779
    ht: &'rc HelperTemplate,
780
    registry: &'reg Registry<'reg>,
781
    ctx: &'rc Context,
782
    rc: &mut RenderContext<'reg, 'rc>,
783
    out: &mut dyn Output,
784
) -> Result<(), RenderError> {
785
    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
11✔
786
    debug!(
×
787
        "Rendering helper: {:?}, params: {:?}, hash: {:?}",
788
        h.name(),
×
789
        h.params(),
×
790
        h.hash()
×
791
    );
792
    let mut call_indent_aware = |helper_def: &dyn HelperDef, rc: &mut RenderContext<'reg, 'rc>| {
25✔
793
        let indent_directive_before = rc.get_indent_before_write();
11✔
794
        let content_produced_before = rc.get_content_produced();
12✔
795
        rc.set_content_produced(false);
10✔
796
        rc.set_indent_before_write(
11✔
797
            indent_directive_before || (ht.indent_before_write && rc.get_trailine_newline()),
12✔
798
        );
799

800
        helper_def.call(&h, registry, ctx, rc, out)?;
12✔
801

802
        if rc.get_content_produced() {
12✔
803
            rc.set_indent_before_write(rc.get_trailine_newline());
15✔
804
        } else {
805
            rc.set_content_produced(content_produced_before);
5✔
806
            rc.set_indent_before_write(indent_directive_before);
5✔
807
        }
808
        Ok(())
11✔
809
    };
810
    if let Some(ref d) = rc.get_local_helper(h.name()) {
35✔
811
        call_indent_aware(&**d, rc)
2✔
812
    } else {
813
        let mut helper = registry.get_or_load_helper(h.name())?;
22✔
814

815
        if helper.is_none() {
25✔
816
            helper = registry.get_or_load_helper(if ht.block {
8✔
817
                BLOCK_HELPER_MISSING
1✔
818
            } else {
819
                HELPER_MISSING
2✔
820
            })?;
821
        }
822

823
        helper
11✔
824
            .ok_or_else(|| RenderErrorReason::HelperNotFound(h.name().to_owned()).into())
16✔
825
            .and_then(|d| call_indent_aware(&*d, rc))
33✔
826
    }
827
}
828

829
pub(crate) fn do_escape(r: &Registry<'_>, rc: &RenderContext<'_, '_>, content: String) -> String {
14✔
830
    if !rc.is_disable_escape() {
29✔
831
        r.get_escape_fn()(&content)
26✔
832
    } else {
833
        content
4✔
834
    }
835
}
836

837
#[inline]
838
pub fn indent_aware_write(
17✔
839
    v: &str,
840
    rc: &mut RenderContext<'_, '_>,
841
    out: &mut dyn Output,
842
) -> Result<(), RenderError> {
843
    if v.is_empty() {
17✔
844
        return Ok(());
6✔
845
    }
846
    rc.set_content_produced(true);
16✔
847

848
    if !v.starts_with(newline_matcher) && rc.get_indent_before_write() {
30✔
849
        if let Some(indent) = rc.get_indent_string() {
4✔
850
            out.write(indent)?;
2✔
851
        }
852
    }
853

854
    if let Some(indent) = rc.get_indent_string() {
17✔
855
        support::str::write_indented(v, indent, out)?;
2✔
856
    } else {
857
        out.write(v.as_ref())?;
15✔
858
    }
859

860
    let trailing_newline = v.ends_with(newline_matcher);
16✔
861
    rc.set_trailing_newline(trailing_newline);
15✔
862
    rc.set_indent_before_write(trailing_newline);
17✔
863

864
    Ok(())
16✔
865
}
866

867
impl Renderable for TemplateElement {
868
    fn render<'reg: 'rc, 'rc>(
19✔
869
        &'rc self,
870
        registry: &'reg Registry<'reg>,
871
        ctx: &'rc Context,
872
        rc: &mut RenderContext<'reg, 'rc>,
873
        out: &mut dyn Output,
874
    ) -> Result<(), RenderError> {
875
        match self {
40✔
876
            RawString(ref v) => indent_aware_write(v.as_ref(), rc, out),
14✔
877
            Expression(ref ht) | HtmlExpression(ref ht) => {
16✔
878
                let is_html_expression = matches!(self, HtmlExpression(_));
12✔
879
                if is_html_expression {
11✔
880
                    rc.set_disable_escape(true);
4✔
881
                }
882

883
                // test if the expression is to render some value
884
                let result = if ht.is_name_only() {
25✔
885
                    let helper_name = ht.name.expand_as_name(registry, ctx, rc)?;
22✔
886
                    if helper_exists(&helper_name, registry, rc) {
25✔
887
                        render_helper(ht, registry, ctx, rc, out)
6✔
888
                    } else {
889
                        debug!("Rendering value: {:?}", ht.name);
×
890
                        let context_json = ht.name.expand(registry, ctx, rc)?;
26✔
891
                        if context_json.is_value_missing() {
21✔
892
                            if registry.strict_mode() {
3✔
893
                                Err(RenderError::strict_error(context_json.relative_path()))
2✔
894
                            } else {
895
                                // helper missing
896
                                if let Some(hook) = registry.get_or_load_helper(HELPER_MISSING)? {
3✔
897
                                    let h = Helper::try_from_template(ht, registry, ctx, rc)?;
3✔
898
                                    hook.call(&h, registry, ctx, rc, out)
2✔
899
                                } else {
900
                                    Ok(())
1✔
901
                                }
902
                            }
903
                        } else {
904
                            let rendered = context_json.value().render();
20✔
905
                            let output = do_escape(registry, rc, rendered);
8✔
906
                            indent_aware_write(output.as_ref(), rc, out)
20✔
907
                        }
908
                    }
909
                } else {
910
                    // this is a helper expression
911
                    render_helper(ht, registry, ctx, rc, out)
10✔
912
                };
913

914
                if is_html_expression {
12✔
915
                    rc.set_disable_escape(false);
8✔
916
                }
917

918
                result
14✔
919
            }
920
            HelperBlock(ref ht) => render_helper(ht, registry, ctx, rc, out),
20✔
921
            DecoratorExpression(_) | DecoratorBlock(_) => self.eval(registry, ctx, rc),
4✔
922
            PartialExpression(ref dt) | PartialBlock(ref dt) => {
10✔
923
                let di = Decorator::try_from_template(dt, registry, ctx, rc)?;
8✔
924

925
                let indent_directive_before = rc.get_indent_before_write();
12✔
926
                let content_produced_before = rc.get_content_produced();
6✔
927

928
                rc.set_indent_before_write(
7✔
929
                    dt.indent_before_write && (rc.get_trailine_newline() || dt.indent.is_some()),
12✔
930
                );
931
                rc.set_content_produced(false);
8✔
932

933
                partial::expand_partial(&di, registry, ctx, rc, out)?;
8✔
934

935
                if rc.get_content_produced() {
7✔
936
                    rc.set_indent_before_write(rc.get_trailine_newline());
17✔
937
                } else {
938
                    rc.set_content_produced(content_produced_before);
2✔
939
                    rc.set_indent_before_write(indent_directive_before);
2✔
940
                }
941
                Ok(())
8✔
942
            }
943
            _ => Ok(()),
2✔
944
        }
945
    }
946
}
947

948
impl Evaluable for TemplateElement {
949
    fn eval<'reg: 'rc, 'rc>(
4✔
950
        &'rc self,
951
        registry: &'reg Registry<'reg>,
952
        ctx: &'rc Context,
953
        rc: &mut RenderContext<'reg, 'rc>,
954
    ) -> Result<(), RenderError> {
955
        match *self {
5✔
956
            DecoratorExpression(ref dt) | DecoratorBlock(ref dt) => {
5✔
957
                let di = Decorator::try_from_template(dt, registry, ctx, rc)?;
4✔
958
                match registry.get_decorator(di.name()) {
8✔
959
                    Some(d) => d.call(&di, registry, ctx, rc),
8✔
960
                    None => Err(RenderErrorReason::DecoratorNotFound(di.name().to_owned()).into()),
1✔
961
                }
962
            }
UNCOV
963
            _ => Ok(()),
×
964
        }
965
    }
966
}
967

968
#[cfg(test)]
969
mod test {
970
    use std::collections::BTreeMap;
971

972
    use super::{Helper, RenderContext, Renderable};
973
    use crate::block::BlockContext;
974
    use crate::context::Context;
975
    use crate::error::RenderError;
976
    use crate::json::path::Path;
977
    use crate::json::value::JsonRender;
978
    use crate::output::{Output, StringOutput};
979
    use crate::registry::Registry;
980
    use crate::template::TemplateElement::*;
981
    use crate::template::{HelperTemplate, Template, TemplateElement};
982

983
    #[test]
984
    fn test_raw_string() {
985
        let r = Registry::new();
986
        let raw_string = RawString("<h1>hello world</h1>".to_string());
987

988
        let mut out = StringOutput::new();
989
        let ctx = Context::null();
990
        {
991
            let mut rc = RenderContext::new(None);
992
            raw_string.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
993
        }
994
        assert_eq!(
995
            out.into_string().unwrap(),
996
            "<h1>hello world</h1>".to_string()
997
        );
998
    }
999

1000
    #[test]
1001
    fn test_expression() {
1002
        let r = Registry::new();
1003
        let element = Expression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1004
            &["hello"],
1005
        ))));
1006

1007
        let mut out = StringOutput::new();
1008
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1009
        let value = "<p></p>".to_string();
1010
        m.insert("hello".to_string(), value);
1011
        let ctx = Context::wraps(&m).unwrap();
1012
        {
1013
            let mut rc = RenderContext::new(None);
1014
            element.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1015
        }
1016

1017
        assert_eq!(
1018
            out.into_string().unwrap(),
1019
            "&lt;p&gt;&lt;/p&gt;".to_string()
1020
        );
1021
    }
1022

1023
    #[test]
1024
    fn test_html_expression() {
1025
        let r = Registry::new();
1026
        let element = HtmlExpression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1027
            &["hello"],
1028
        ))));
1029

1030
        let mut out = StringOutput::new();
1031
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1032
        let value = "world";
1033
        m.insert("hello".to_string(), value.to_string());
1034
        let ctx = Context::wraps(&m).unwrap();
1035
        {
1036
            let mut rc = RenderContext::new(None);
1037
            element.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1038
        }
1039

1040
        assert_eq!(out.into_string().unwrap(), value.to_string());
1041
    }
1042

1043
    #[test]
1044
    fn test_template() {
1045
        let r = Registry::new();
1046
        let mut out = StringOutput::new();
1047
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1048
        let value = "world".to_string();
1049
        m.insert("hello".to_string(), value);
1050
        let ctx = Context::wraps(&m).unwrap();
1051

1052
        let elements: Vec<TemplateElement> = vec![
1053
            RawString("<h1>".to_string()),
1054
            Expression(Box::new(HelperTemplate::with_path(Path::with_named_paths(
1055
                &["hello"],
1056
            )))),
1057
            RawString("</h1>".to_string()),
1058
            Comment(String::new()),
1059
        ];
1060

1061
        let template = Template {
1062
            elements,
1063
            name: None,
1064
            mapping: Vec::new(),
1065
        };
1066

1067
        {
1068
            let mut rc = RenderContext::new(None);
1069
            template.render(&r, &ctx, &mut rc, &mut out).ok().unwrap();
1070
        }
1071

1072
        assert_eq!(out.into_string().unwrap(), "<h1>world</h1>".to_string());
1073
    }
1074

1075
    #[test]
1076
    fn test_render_context_promotion_and_demotion() {
1077
        use crate::json::value::to_json;
1078
        let mut render_context = RenderContext::new(None);
1079
        let mut block = BlockContext::new();
1080

1081
        block.set_local_var("index", to_json(0));
1082
        render_context.push_block(block);
1083

1084
        render_context.push_block(BlockContext::new());
1085
        assert_eq!(
1086
            render_context.get_local_var(1, "index").unwrap(),
1087
            &to_json(0)
1088
        );
1089

1090
        render_context.pop_block();
1091

1092
        assert_eq!(
1093
            render_context.get_local_var(0, "index").unwrap(),
1094
            &to_json(0)
1095
        );
1096
    }
1097

1098
    #[test]
1099
    fn test_render_subexpression_issue_115() {
1100
        use crate::support::str::StringWriter;
1101

1102
        let mut r = Registry::new();
1103
        r.register_helper(
1104
            "format",
1105
            Box::new(
1106
                |h: &Helper<'_>,
1107
                 _: &Registry<'_>,
1108
                 _: &Context,
1109
                 _: &mut RenderContext<'_, '_>,
1110
                 out: &mut dyn Output|
1111
                 -> Result<(), RenderError> {
1112
                    out.write(&h.param(0).unwrap().value().render())
1113
                        .map_err(RenderError::from)
1114
                },
1115
            ),
1116
        );
1117

1118
        let mut sw = StringWriter::new();
1119
        let mut m: BTreeMap<String, String> = BTreeMap::new();
1120
        m.insert("a".to_string(), "123".to_string());
1121

1122
        {
1123
            if let Err(e) = r.render_template_to_write("{{format (format a)}}", &m, &mut sw) {
1124
                panic!("{}", e);
1125
            }
1126
        }
1127

1128
        assert_eq!(sw.into_string(), "123".to_string());
1129
    }
1130

1131
    #[test]
1132
    fn test_render_error_line_no() {
1133
        let mut r = Registry::new();
1134
        let m: BTreeMap<String, String> = BTreeMap::new();
1135

1136
        let name = "invalid_template";
1137
        assert!(r
1138
            .register_template_string(name, "<h1>\n{{#if true}}\n  {{#each}}{{/each}}\n{{/if}}")
1139
            .is_ok());
1140

1141
        if let Err(e) = r.render(name, &m) {
1142
            assert_eq!(e.line_no.unwrap(), 3);
1143
            assert_eq!(e.column_no.unwrap(), 3);
1144
            assert_eq!(e.template_name, Some(name.to_owned()));
1145
        } else {
1146
            panic!("Error expected");
1147
        }
1148
    }
1149

1150
    #[test]
1151
    fn test_partial_failback_render() {
1152
        let mut r = Registry::new();
1153

1154
        assert!(r
1155
            .register_template_string("parent", "<html>{{> layout}}</html>")
1156
            .is_ok());
1157
        assert!(r
1158
            .register_template_string(
1159
                "child",
1160
                "{{#*inline \"layout\"}}content{{/inline}}{{#> parent}}{{> seg}}{{/parent}}",
1161
            )
1162
            .is_ok());
1163
        assert!(r.register_template_string("seg", "1234").is_ok());
1164

1165
        let r = r.render("child", &true).expect("should work");
1166
        assert_eq!(r, "<html>content</html>");
1167
    }
1168

1169
    #[test]
1170
    fn test_key_with_slash() {
1171
        let mut r = Registry::new();
1172

1173
        assert!(r
1174
            .register_template_string("t", "{{#each this}}{{@key}}: {{this}}\n{{/each}}")
1175
            .is_ok());
1176

1177
        let r = r.render("t", &json!({"/foo": "bar"})).unwrap();
1178

1179
        assert_eq!(r, "/foo: bar\n");
1180
    }
1181

1182
    #[test]
1183
    fn test_comment() {
1184
        let r = Registry::new();
1185

1186
        assert_eq!(
1187
            r.render_template("Hello {{this}} {{! test me }}", &0)
1188
                .unwrap(),
1189
            "Hello 0 "
1190
        );
1191
    }
1192

1193
    #[test]
1194
    fn test_zero_args_heler() {
1195
        let mut r = Registry::new();
1196

1197
        r.register_helper(
1198
            "name",
1199
            Box::new(
1200
                |_: &Helper<'_>,
1201
                 _: &Registry<'_>,
1202
                 _: &Context,
1203
                 _: &mut RenderContext<'_, '_>,
1204
                 out: &mut dyn Output|
1205
                 -> Result<(), RenderError> {
1206
                    out.write("N/A").map_err(Into::into)
1207
                },
1208
            ),
1209
        );
1210

1211
        r.register_template_string("t0", "Output name: {{name}}")
1212
            .unwrap();
1213
        r.register_template_string("t1", "Output name: {{first_name}}")
1214
            .unwrap();
1215
        r.register_template_string("t2", "Output name: {{./name}}")
1216
            .unwrap();
1217

1218
        // when "name" is available in context, use context first
1219
        assert_eq!(
1220
            r.render("t0", &json!({"name": "Alex"})).unwrap(),
1221
            "Output name: N/A"
1222
        );
1223

1224
        // when "name" is unavailable, call helper with same name
1225
        assert_eq!(
1226
            r.render("t2", &json!({"name": "Alex"})).unwrap(),
1227
            "Output name: Alex"
1228
        );
1229

1230
        // output nothing when neither context nor helper available
1231
        assert_eq!(
1232
            r.render("t1", &json!({"name": "Alex"})).unwrap(),
1233
            "Output name: "
1234
        );
1235

1236
        // generate error in strict mode for above case
1237
        r.set_strict_mode(true);
1238
        assert!(r.render("t1", &json!({"name": "Alex"})).is_err());
1239

1240
        // output nothing when helperMissing was defined
1241
        r.set_strict_mode(false);
1242
        r.register_helper(
1243
            "helperMissing",
1244
            Box::new(
1245
                |h: &Helper<'_>,
1246
                 _: &Registry<'_>,
1247
                 _: &Context,
1248
                 _: &mut RenderContext<'_, '_>,
1249
                 out: &mut dyn Output|
1250
                 -> Result<(), RenderError> {
1251
                    let name = h.name();
1252
                    write!(out, "{name} not resolved")?;
1253
                    Ok(())
1254
                },
1255
            ),
1256
        );
1257
        assert_eq!(
1258
            r.render("t1", &json!({"name": "Alex"})).unwrap(),
1259
            "Output name: first_name not resolved"
1260
        );
1261
    }
1262

1263
    #[test]
1264
    fn test_identifiers_starting_with_numbers() {
1265
        let mut r = Registry::new();
1266

1267
        assert!(r
1268
            .register_template_string("r1", "{{#if 0a}}true{{/if}}")
1269
            .is_ok());
1270
        let r1 = r.render("r1", &json!({"0a": true})).unwrap();
1271
        assert_eq!(r1, "true");
1272

1273
        assert!(r.register_template_string("r2", "{{eq 1a 1}}").is_ok());
1274
        let r2 = r.render("r2", &json!({"1a": 2, "a": 1})).unwrap();
1275
        assert_eq!(r2, "false");
1276

1277
        assert!(r
1278
            .register_template_string("r3", "0: {{0}} {{#if (eq 0 true)}}resolved from context{{/if}}\n1a: {{1a}} {{#if (eq 1a true)}}resolved from context{{/if}}\n2_2: {{2_2}} {{#if (eq 2_2 true)}}resolved from context{{/if}}") // YUP it is just eq that barfs! is if handled specially? maybe this test should go nearer to specific helpers that fail?
1279
            .is_ok());
1280
        let r3 = r
1281
            .render("r3", &json!({"0": true, "1a": true, "2_2": true}))
1282
            .unwrap();
1283
        assert_eq!(
1284
            r3,
1285
            "0: true \n1a: true resolved from context\n2_2: true resolved from context"
1286
        );
1287

1288
        // these should all be errors:
1289
        assert!(r.register_template_string("r4", "{{eq 1}}").is_ok());
1290
        assert!(r.register_template_string("r5", "{{eq a1}}").is_ok());
1291
        assert!(r.register_template_string("r6", "{{eq 1a}}").is_ok());
1292
        assert!(r.render("r4", &()).is_err());
1293
        assert!(r.render("r5", &()).is_err());
1294
        assert!(r.render("r6", &()).is_err());
1295
    }
1296

1297
    #[test]
1298
    fn test_recursive_path_resolution() {
1299
        let mut r = Registry::new();
1300
        const TEMPLATE: &str = "{{#each children}}outer={{{outer}}} inner={{{inner}}}{{/each}}";
1301

1302
        let data = json!({
1303
          "children": [{"inner": "inner"}],
1304
          "outer": "outer"
1305
        });
1306

1307
        let r1 = r.render_template(TEMPLATE, &data).unwrap();
1308
        assert_eq!(r1, "outer= inner=inner");
1309

1310
        r.set_recursive_lookup(true);
1311

1312
        let r2 = r.render_template(TEMPLATE, &data).unwrap();
1313
        assert_eq!(r2, "outer=outer inner=inner");
1314
    }
1315
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc