Handlebars whitespace-controlled partial invocation stripping rules with nested tilde tags
0 reputation · 28 Jul 2023, 03:34 UTC
Goal
Achieve predictable whitespace output when a whitespace-controlled partial invocation ({{~> partial}}) wraps a partial that itself begins or ends with tilde-prefixed tags ({{~expression}} or {{~#block}}).
Constraints and uncertainty
The Handlebars specification does not define how adjacent tilde directives interact across partial boundaries. The JavaScript reference implementation treats ~ as a lexer-level horizontal-whitespace consumer, while handlebars-rust may consume vertical whitespace in some contexts. When {{~> itemPartial}} appears inside a whitespace-controlled block helper such as {{~#each}}, implementations differ on whether the outer tilde consumes the newline before the partial's first tag or after its last tag. No official test suite covers these nested scenarios, leaving cross-runtime portability unverified.
Questions
- Does
{{~> partial}}strip the newline preceding the partial's first tilde tag in handlebars.js 4.7.8, 5.0.0, and handlebars-rust 4.0? - When a partial starts with
{{~content}}, does the invocation's leading tilde merge with the partial's tilde or operate independently? - Is there a documented convention for authoring partials that behave consistently whether invoked with
{{>}}or{{~>}}?