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

Indenting only first line #1138

Open
edwardaux opened this issue Aug 7, 2024 · 1 comment
Open

Indenting only first line #1138

edwardaux opened this issue Aug 7, 2024 · 1 comment

Comments

@edwardaux
Copy link

I've been looking at using handlebars.java to generated some Kotlin/Swift source code (awesome library, BTW!) and have run afoul of a strange indenting problem. Note that I need to use prettyPrint otherwise my generated source code is really difficult to read (as a human)

Consider the following code:

val handlebars = Handlebars()
handlebars.setPrettyPrint(true)
val template = handlebars.compileInline(
    """
    ---
        {{#list}}/// {{.}}
        {{/list}}
    ---
    {{#list}}/// {{.}}
    {{/list}}
    ---
    """.trimIndent()
)
println(template.apply(mapOf("list" to listOf("one", "two", "three", "four"))))

If I run as-is, I see:

---
    /// one
/// two
/// three
/// four
---
/// one
/// two
/// three
/// four
---

However, if I use setPrettyPrint(false), then I see:

---
    /// one
    /// two
    /// three
    /// four
    
---
/// one
/// two
/// three
/// four

---

I'm trying to figure out why using prettyPrint gives me the first result? I noted a similar ticket in #401 (I initially stumbled across this problem using a partial, but I've managed to reproduce just using a standalone template) but it was closed by the OP without any more info about what they did to solve it.

Reading the doco for prettyPrint suggests that there's some rules in the Mustache spec that it is following, but I'm struggling to find where in the spec so I can tweak my template accordingly.

I'm not convinced that this is a bug in this project (it is probably user error)... but, even if so, I'm super keen to figure out why it is behaving like this and more importantly how I can work around it so that they're all indented (maybe I need the opposite of ~)

Any help would be awesome. Thanks.

@edwardaux
Copy link
Author

edwardaux commented Aug 7, 2024

One more thing that I did note that was even more confusing is that, even when using prettyPrint, if I appended something (any non-whitespace char) AFTER the end of my section, then the indentation of the whole section works as expected. For example, with a template that looks like:

---
    {{#list}}/// {{.}}
    {{/list}}x
---
{{#list}}/// {{.}}
{{/list}}
---

it now outputs:

---
    /// one
    /// two
    /// three
    /// four
    x
---
/// one
/// two
/// three
/// four
---

I don't know how having an x character AFTER the {{/list}} marker affects everything INSIDE the list but I'm sure that's just my lack of knowledge showing... 🤔

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

No branches or pull requests

1 participant