ractivejs / ractive

Next-generation DOM manipulation
http://ractive.js.org
MIT License
5.94k stars 396 forks source link

Optionality for partials #3337

Open daytonlowell opened 4 years ago

daytonlowell commented 4 years ago

Description:

Currently, if you have a partial that never gets referenced, Ractive will put a warning in the console like

Could not find template for partial '<named yield>'

Sometimes I would like to mark a partial as optional. This comes up most often in components where I'll created named yields that the consumer of the component may not be interested in actually using(depending on their use-case).

Perhaps something like {{yield?}} and {{>?}} would suppress the warning.

Versions affected:

all

Platforms affected:

all

Reproduction:

N/A

MartinKolarik commented 4 years ago

👍 this is something I've run into as well and should be fairly simple to add.

evs-chris commented 4 years ago

It just occurred to me that you could also supply a default partial (see foo) in the component to avoid this e.g.

Ractive.extend({
  template: '<div>{{yield something}}</div>',
  partials: {
    something: []
  }
});

Would that be an acceptable workaround, or is optional syntax more clear here? From the perspective of a function call, function fn(foo?: Foo) and function fn(foo: Foo = {}) are subtly but definitely different.

daytonlowell commented 4 years ago

The optional syntax may be more clear, but supplying a default partial totally works for my use case. Thanks.