Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partials do not respect the indentation from source content #482

Closed
nswarm opened this issue Dec 27, 2021 · 7 comments · Fixed by #486
Closed

Partials do not respect the indentation from source content #482

nswarm opened this issue Dec 27, 2021 · 7 comments · Fixed by #486
Milestone

Comments

@nswarm
Copy link

nswarm commented Dec 27, 2021

Partial calls only use indentation defined in the partial template itself.

Example

inner.hbs

name: {{name}}

outer.hbs

                {{> inner inner_solo}}

{{#each inners}}
                {{> inner}}
{{/each}}

        {{#each inners}}
        {{> inner}}
        {{/each}}

main.rs

use std::error::Error;
use handlebars::Handlebars;
use serde_json::json;

fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();
    let mut hbs = Handlebars::new();

    hbs.register_template_file("inner", "./templates/inner.hbs")?;
    hbs.register_template_file("outer", "./templates/outer.hbs")?;

    let result = hbs.render("outer", &json!({
        "inner_solo": {"name": "inner_solo"},
        "inners": [
            {"name": "hello"},
            {"name": "there"}
        ]
    }))?;
    println!("{}", result);

    Ok(())
}

Result:

name: inner_solo

name: hello
name: there

name: hello
name: there

Handlebars JS Handling

Handlebars js by default uses the indentation of the partial call itself, but has a compiler flag to disable this functionality, see preventIndent in the Handlebars.compile options.

Use Case

I'm using recursively nested partials to render a tree view, so specifying indentation in the target template itself does not work.

--

Thanks for your hard work!

@nswarm
Copy link
Author

nswarm commented Dec 27, 2021

I've worked around my use case for with a custom block helper that explicitly injects indentation into its rendered content.

@sunng87
Copy link
Owner

sunng87 commented Dec 28, 2021

Thank you for reporting! I will look into my template parser to see if preventIndent can be supported.

@TheButlah
Copy link

TheButlah commented Apr 29, 2022

#486 does not appear to have actually solved this. If the text put into the partial spans multiple lines, the next line does not keep the same indentation as the first line.

Also, it appears as though none of the tests for indentation in partials test what happens if the string that is going to be used for the render spans multiple lines.

@TheButlah
Copy link

Can this please be reopened? Or should I file a new issue?

@sunng87
Copy link
Owner

sunng87 commented May 12, 2022

@TheButlah could you please provide me a demo template for reproducing this?

@sunng87
Copy link
Owner

sunng87 commented May 14, 2022

I've created #504 to track this.

@TheButlah
Copy link

@TheButlah could you please provide me a demo template for reproducing this?

Sorry, I just saw this! It looks like you solved it though :)
I'll post in #504 if I have any further bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants