Skip to content

Helper arguments starting with a number are split in two parts #450

@jdesgats

Description

@jdesgats

When a helper parameter starts with a number, but contain something else after, it will be split at the first non-digit character.

For example {{helper 100_000}} will result in an agument sequence of [PathAndJson { relative_path: None, value: Constant(Number(100)) }, PathAndJson { relative_path: Some("_000"), value: Missing }]

I would expect one of:

  • String "100_000"
  • Number 100000
  • Syntax error

But truncating here seems like a bug.

Full example:

use std::collections::BTreeMap;
use handlebars::{Handlebars, RenderContext, Helper, Context, JsonRender, HelperResult, Output};

fn echo_helper (h: &Helper, _: &Handlebars, _: &Context, rc: &mut RenderContext, out: &mut dyn Output) -> HelperResult {
    println!("params={:?}", h.params());
    let param = h.param(0).unwrap();
    out.write(param.value().render().as_ref())?;
    Ok(())
}

fn main() {
  // create the handlebars registry
  let mut handlebars = Handlebars::new();
  handlebars.register_helper("echo", Box::new(echo_helper));

  // register the template. The template string will be verified and compiled.
  let source = "amount: {{echo 100_000}}";
  assert!(handlebars.register_template_string("t1", source).is_ok());

  // Prepare some data.
  //
  // The data type should implements `serde::Serialize`
  let data: BTreeMap<String, String> = BTreeMap::new();
  assert_eq!(handlebars.render("t1", &data).unwrap(), "amount: 100_000");
}

Output with handlebars-rust 4.0.1:

params=[PathAndJson { relative_path: None, value: Constant(Number(100)) }, PathAndJson { relative_path: Some("_000"), value: Missing }]
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"amount: 100"`,
 right: `"amount: 100_000"`', src/main.rs:24:3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions