handlebars-rust icon indicating copy to clipboard operation
handlebars-rust copied to clipboard

extra whitespaces after \n in partial when rendered from a partial

Open ryanolson opened this issue 10 months ago • 2 comments

version: 4.4

    #[test]
    fn test_partial_using_partial() {
        let mut handlebars = Handlebars::new();
        handlebars
            .register_partial("system", "{{#if system_prompt}}{{{system_prompt}}} {{/if}}")
            .unwrap();

        handlebars
            .register_partial("user", "User: {{>system}}{{{prompt}}}")
            .unwrap();

        let output = handlebars
            .render_template("{{>user system_prompt=\"hello\" prompt=\"world\"}}", &())
            .unwrap();

        assert_eq!(output, "User: hello world");

        let output = handlebars
            .render_template("{{>user prompt=\"world\"}}", &())
            .unwrap();

        assert_eq!(output, "User: world");

        let output = handlebars
            .render_template("{{>user}}", &json!({"system_prompt": "<<SYS>>\nhello\n<</SYS>>\n\n", "prompt": "world"}))
            .unwrap();

        assert_eq!(output, "User: <<SYS>>\nhello\n<</SYS>>\n\n world");
    }

The last assert fails. The rendered string vs the expected:

  left: `"User: <<SYS>>\n hello\n <</SYS>>\n \n world"`,
 right: `"User: <<SYS>>\nhello\n<</SYS>>\n\n world"`',

ryanolson avatar Sep 29 '23 08:09 ryanolson

playground link

ryanolson avatar Sep 30 '23 01:09 ryanolson

Note: this code works as expected using 3.5.5

ryanolson avatar Sep 30 '23 06:09 ryanolson