Skip to content

Commit 1bf4b62

Browse files
praihanfacebook-github-bot
authored andcommitted
Update partial spec to drop "as"
Summary: After discussion with vitaut, we've decided to drop `as` before the arguments because of its resemblance with iteration (`each`). The other change is the diff is clarification of the name binding that happens and an example to showcase the behavior. Reviewed By: yoney Differential Revision: D68864499 fbshipit-source-id: 7d98f6d970efd6609aa8af57d34cbcd11022895e
1 parent a106994 commit 1bf4b62

File tree

1 file changed

+160
-20
lines changed
  • third-party/thrift/src/thrift/doc/contributions

1 file changed

+160
-20
lines changed

third-party/thrift/src/thrift/doc/contributions/whisker.md

+160-20
Original file line numberDiff line numberDiff line change
@@ -624,36 +624,180 @@ pragma-statement → { "{{" ~ "#" ~ "pragma" ~ ( "single-line" ) ~ "}}" }
624624

625625
### Partial Blocks & Statements
626626

627-
:::warning
628-
`{{#let partial}}` blocks and `{{#partial}}` statements have not been implemented yet.
629-
:::
630-
631-
Partial blocks allow defining reusable templates within a Whisker template. They are not rendered unless *applied* (by name). A simple example of a `{{#let partial}}` block might be:
627+
Partial blocks allow defining reusable templates within a Whisker template. They are not rendered unless *applied* (by name).
628+
The following example of a `{{#let partial}}` block defines a partial named `greeting` that accepts a single argument named `person`:
632629

633630
```handlebars
634-
{{#let partial greeting as |person|}}
631+
{{#let partial greeting |person|}}
635632
Greetings, {{person.firstName}} {{person.lastName}}!
636633
{{/let partial}}
637634
```
638635

639-
Partial blocks must be applied with `{{#partial ...}}` statements. A simple example for a `{{#partial}}` statement for the above block might be:
636+
Partial blocks must be rendered using `{{#partial ...}}` statements. A simple example of a `{{#partial}}` statement for the above block might be:
640637

641638
```handlebars
642639
{{#partial greeting person=person}}
643640
```
644641

645-
The `{{#partial}}` statement must include named arguments that are [bound](#scopes) to `expression`s, matching the captures (`as |...|`) from the definition.
642+
The `{{#partial}}` statement must include all named arguments from the partial block being applied.
643+
Each named argument is an `expression`, which is [bound](#scopes) to the corresponding argument (matching `|...|`) from the partial block.
646644

647645
The contained body of the `{{#let partial}}` block is rendered with a [derived evaluation context](#derived-evaluation-context). Names accessible from the site of the application are **not** *implicitly* available within the block.
648646

649647
<Example>
650648

651649
```handlebars
652-
{{#let partial greeting as |person|}}
650+
{{#let partial greeting |person|}}
653651
Greetings, {{person.firstName}} {{person.lastName}}!
654652
{{/let partial}}
655653
656-
{{> greeting person=dave}}
654+
{{#partial greeting person=dave}}
655+
```
656+
657+
```json title=Context
658+
{
659+
"dave": {
660+
"firstName": "Dave",
661+
"lastName": "Grohl"
662+
}
663+
}
664+
```
665+
666+
```text title=Output
667+
Greetings, Dave Grohl!
668+
```
669+
670+
</Example>
671+
672+
To implement recursive partials, a partial can access itself by name.
673+
674+
<Example title="Example with recursion">
675+
676+
```handlebars
677+
{{! https://en.wikipedia.org/wiki/Collatz_conjecture }}
678+
{{#let partial collatz |n|}}
679+
{{n}}
680+
{{#if (ne? n 1)}}
681+
{{#if (even? n)}}
682+
{{#partial collatz n=(div n 2)}}
683+
{{#else}}
684+
{{#partial collatz n=(add (mul 3 n) 1)}}
685+
{{/if (even? n)}}
686+
{{/if (ne? n 1)}}
687+
{{/let partial}}
688+
{{#partial collatz n=6}}
689+
```
690+
691+
```javascript title=Globals
692+
{
693+
"even?": (n) => n % 2 == 0,
694+
"mul": (a, b) => a * b,
695+
"div": (a, b) => a / b,
696+
"ne?": (a, b) => a != b,
697+
"add": (a, b) => a + b,
698+
}
699+
```
700+
701+
```text title=Output
702+
6
703+
3
704+
10
705+
5
706+
16
707+
8
708+
4
709+
2
710+
1
711+
```
712+
713+
</Example>
714+
715+
Partial blocks and statements do not require arguments.
716+
717+
<Example title="Example without arguments">
718+
719+
```handlebars
720+
{{#let partial copyright}}
721+
/*
722+
* Copyright (c) Meta Platforms, Inc. and affiliates.
723+
*
724+
* Licensed under the Apache License, Version 2.0 (the "License");
725+
* you may not use this file except in compliance with the License.
726+
* You may obtain a copy of the License at
727+
*
728+
* http://www.apache.org/licenses/LICENSE-2.0
729+
*
730+
* Unless required by applicable law or agreed to in writing, software
731+
* distributed under the License is distributed on an "AS IS" BASIS,
732+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
733+
* See the License for the specific language governing permissions and
734+
* limitations under the License.
735+
*/
736+
{{/let partial}}
737+
{{#partial copyright}}
738+
```
739+
740+
```text title=Output
741+
/*
742+
* Copyright (c) Meta Platforms, Inc. and affiliates.
743+
*
744+
* Licensed under the Apache License, Version 2.0 (the "License");
745+
* you may not use this file except in compliance with the License.
746+
* You may obtain a copy of the License at
747+
*
748+
* http://www.apache.org/licenses/LICENSE-2.0
749+
*
750+
* Unless required by applicable law or agreed to in writing, software
751+
* distributed under the License is distributed on an "AS IS" BASIS,
752+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
753+
* See the License for the specific language governing permissions and
754+
* limitations under the License.
755+
*/
756+
```
757+
758+
</Example>
759+
760+
Partial blocks may have multiple arguments. The arguments may be provided in any order.
761+
762+
<Example title="Example with multiple arguments">
763+
764+
```handlebars
765+
{{#let partial greeting |firstName lastName|}}
766+
Greetings, {{firstName}} {{lastName}}!
767+
{{/let partial}}
768+
769+
{{#partial greeting lastName=dave.lastName firstName=dave.firstName}}
770+
```
771+
772+
```json title=Context
773+
{
774+
"dave": {
775+
"firstName": "Dave",
776+
"lastName": "Grohl"
777+
}
778+
}
779+
```
780+
781+
```text title=Output
782+
Greetings, Dave Grohl!
783+
```
784+
785+
</Example>
786+
787+
The name of a partial block is [bound](#scopes) to an [object](#data-model) in the current evaluation context, meaning it follows normal [name resolution rules](#evaluation-context). As a result, a partial block can be passed around like any other object.
788+
789+
<Example title="Example with partial as object">
790+
791+
```handlebars
792+
{{#let partial greeting |firstName lastName|}}
793+
Greetings, {{firstName}} {{lastName}}!
794+
{{/let partial}}
795+
796+
{{#let partial with-firstName-lastName |person action|}}
797+
{{#partial action firstName=person.firstName lastName=person.lastName}}
798+
{{/let partial}}
799+
800+
{{#partial with-firstName-lastName person=dave action=greeting}}
657801
```
658802

659803
```json title=Context
@@ -676,7 +820,7 @@ Partial statements retain the *preceding indentation* at the site of the applica
676820
<Example title="Example with indentation">
677821

678822
```handlebars title=example.whisker
679-
{{#let partial president as |person|}}
823+
{{#let partial president |person|}}
680824
{{person.lastName}}
681825
{{person.firstName}}
682826
{{/let partial}}
@@ -715,21 +859,17 @@ Some historic presidents are:
715859
<Grammar>
716860

717861
```
718-
partial-block → { partial-block-open ~ body* ~ partial-block-close }
719-
partial-block-open → { "{{#" ~ "let" ~ "partial" ~ identifier ~ partial-block-capture ~ "}}" }
720-
partial-block-capture → { "as" ~ "|" ~ identifier+ ~ "|" }
721-
partial-block-close → { "{{/" ~ "let" ~ "partial" ~ "}}" }
862+
partial-block → { partial-block-open ~ body* ~ partial-block-close }
863+
partial-block-open → { "{{#" ~ "let" ~ "partial" ~ identifier ~ partial-block-arguments? ~ "}}" }
864+
partial-block-arguments → { "|" ~ identifier+ ~ "|" }
865+
partial-block-close → { "{{/" ~ "let" ~ "partial" ~ "}}" }
722866
723-
partial-statement → { "{{" ~ "#" ~ "partial" ~ identifier ~ partial-argument+ ~ "}}" }
867+
partial-statement → { "{{" ~ "#" ~ "partial" ~ expression ~ partial-argument* ~ "}}" }
724868
partial-argument → { identifier ~ "=" ~ expression }
725869
```
726870

727871
</Grammar>
728872

729-
:::note
730-
Partial blocks may only appear at the top of a source file.
731-
:::
732-
733873
Whisker `{{#let partial}}` blocks are based on [Handlebars partial parameters](https://handlebarsjs.com/guide/partials.html#partial-parameters).
734874

735875
### Macros

0 commit comments

Comments
 (0)