handlebars-lang / handlebars-lang/handlebars.js
Add a helper to check whether a partial is defined
- Dominant language
- JavaScript
- Stars
- 18.7k
- Forks
- 2.1k
- PR merge metrics
- No merged PRs in 30d
Description
This was [previously discussed here](https://github.com/handlebars-lang/handlebars.js/discussions/1783). The conclusion was that there currently is no way to check whether a partial is defined, so I'm making a feature request here.
This is mainly useful for inline partials, like the [example from the docs](https://handlebarsjs.com/guide/partials.html#inline-partials).
If you define a partial `layout` like this:
```hbs
{{> content}}
```
it basically has 2 "template parameters" that are passed to the partial using inline partials:
```hbs
{{#> layout}}
{{#*inline "nav"}}
My Nav
{{/inline}}
{{#*inline "content"}}
My Content
{{/inline}}
{{/layout}}
```
Now I want to make the `nav` parameter in my `layout` partial optional. I can use `{{#> nav}}{{/nav}}` to avoid an error if the partial is missing. But this will still leave the enclosing `
` in the output. To avoid this, I need a way to check if the `nav` partial is defined, for example `{{#if (partialDefined "nav")}}`. Helpers also don't receive any information about existing partials, so implementing a custom helper for this isn't currently possible either.----
In summary, I'd like to be able to do this:
```hbs
{{#if (partialDefined "nav")}}
{{/if}}
{{> content}}
```
```hbs
{{#> layout}}
{{#*inline "content"}}
My Content
{{/inline}}
{{/layout}}
```
Contributor guide
Research direction
Start with the inline-partial example in the linked Handlebars documentation and the proposed partialDefined usage. The issue names no repository files or tests, so trace how partials and helpers are exposed before defining behavior for missing and inline partials. Done means the conditional can detect a defined partial and avoid rendering its wrapper when absent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- handlebars, javascript
- Domain
- web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100